2013-01-18 2 views
2

내 응용 프로그램의 경우 로컬 네트워크에있는 서버에서 호스팅되는 웹 페이지에서 최신 데이터를 가져와야합니다.안드로이드 : 빠른 웹 요청

그래서 나는 HTTP GET으로 최신 페이지를 요청하고 데이터가 수신되면 다른 요청을 보냅니다.

현재 구현으로 요청 당 100 - 120ms 정도 가깝습니다. 요청한 것과 동일한 URL이기 때문에이 작업을 더 빨리 수행 할 수 있습니까?

예를 들어, 새 연결을 설정하지 않고 페이지에 연결을 유지하고 최신 데이터를 grep 하시겠습니까?

이 페이지는 900-1100 바이트입니다.

HTTP GET 코드 :

public static String makeHttpGetRequest(String stringUrl) { 

    try { 
     URL url = new URL(stringUrl); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
     con.setReadTimeout(300); 
     con.setConnectTimeout(300); 
     con.setDoOutput(false); 
     con.setDoInput(true); 
     con.setChunkedStreamingMode(0); 
     con.setRequestMethod("GET"); 

     return readStream(con.getInputStream()); 
    } catch (IOException e) { 
     Log.e(TAG, "IOException when setting up connection: " + e.getMessage()); 
    } 
    return null; 
} 

읽기의 InputStream

private static String readStream(InputStream in) { 
    BufferedReader reader = null; 
    StringBuilder total = new StringBuilder(); 
    try { 
     String line = ""; 
     reader = new BufferedReader(new InputStreamReader(in)); 
     while ((line = reader.readLine()) != null) { 
      total.append(line); 
     } 
    } catch (IOException e) { 
     Log.e(TAG, "IOException when reading InputStream: " + e.getMessage()); 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    return total.toString(); 
} 

답변

0

나는 당신이 요구하는 등의 구현이 아니라는 것을 알고있다. 나는 HTTP 요청을 많이 다루었으며 당신이 할 수있는 최선의 일은 코드이다. 몇 가지주의가 필요한 다른 것이 있습니다 ... 연결 속도가 느릴 수 있으며 연결 시간에 따라 더 많거나 적을 수도 있습니다. 연결 시간 제한이 충분하지 않지만 서버 문제입니다.

제 의견으로는 지금 가지고있는 것을 사용해야합니다.