내 응용 프로그램의 경우 로컬 네트워크에있는 서버에서 호스팅되는 웹 페이지에서 최신 데이터를 가져와야합니다.안드로이드 : 빠른 웹 요청
그래서 나는 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();
}