2011-10-17 2 views
1

다음 코드를 사용하여 http 서버에서 일부 텍스트를 검색합니다. 크기는 1kB 미만이고 0.002 밀리 초 단위로 생성됩니다.Android url.openStream slow?

그러나 데이터를 가져 오는 데는 600 밀리 초가 걸리지 만 대개는 2000 밀리 초와 5000 밀리 초 사이가 걸릴 수 있습니다.

다음과 같은 코드가 사용된다 :

long starttime = System.currentTimeMillis(); 
     StringBuffer SB = new StringBuffer(); 
     Common.toLog("101 took "+ (System.currentTimeMillis() - starttime) + " ms"); 
     try { 
      URL url = new URL(Common.server+request); 
      Common.toLog("102 took "+ (System.currentTimeMillis() - starttime) + " ms"); 
      InputStreamReader ISR = new InputStreamReader(url.openStream()); 
      Common.toLog("102a took "+ (System.currentTimeMillis() - starttime) + " ms"); 
      BufferedReader in = new BufferedReader(ISR); 
      Common.toLog("103 took "+ (System.currentTimeMillis() - starttime) + " ms"); 
      String inputLine; 
      while ((inputLine = in.readLine()) != null) { 
       SB.append(inputLine); 
      } 

      in.close(); 
      Common.toLog("105 took "+ (System.currentTimeMillis() - starttime) + " ms"); 
     } catch (IOException e) 
     { 
      Common.toLog("Could not make connection 1"); 
      showMSG(R.string.ERROR_NO_INTERNET, true); 
     } 

가장 시간이 많이 걸리는 방법은 로그 - 포인트 (102)과 점 (102A) 사이에있다. 크롬을 사용하면 the page을 300-350ms 내에로드 할 수 있습니다. 이 데이터를보다 효율적으로 검색 할 수있는 방법이 있는지 알고 싶습니다.

답변

0

가져 오는 데이터는 그리 길지 않습니다. 대신 URL에 읽기 스트림을 여는, HttpRequest에의 구현 중 하나를 사용해보십시오 : 하나의 요청이

http://developer.android.com/reference/org/apache/http/HttpRequest.html

이 방법을 만들어 귀하의 데이터는 다시 응답 올 것입니다. http://androidforums.com/application-development/9261-doing-http-request-android :

HttpGet httpGet = new HttpGet(url); 
HttpClient httpclient = new DefaultHttpClient(); 
// Execute HTTP Get Request 
HttpResponse response = httpclient.execute(httpGet); 
content = response.getEntity().getContent(); 
+0

그것은 다음과 같은 스크립트를 사용하여 이전보다 더 예를 들어, 당신은 HttpGet 요청을 시도 할 수 있습니다. html 그러나 : 첫 번째 요청에 대한 응답 시간은 다음 요청에 대해 +/- 700 초에 비해 1600ms가 걸립니다. –