Android:快速网络请求
对于我的应用程序,我需要从托管在本地网络服务器上的网页获取最新数据 因此,我使用Android:快速网络请求,android,httpurlconnection,http-get,Android,Httpurlconnection,Http Get,对于我的应用程序,我需要从托管在本地网络服务器上的网页获取最新数据 因此,我使用httpget请求最新页面,当收到数据时,我发送另一个请求 在我当前的实现中,每个请求的时间大约为100-120毫秒。是否有可能使这更快,因为它是相同的url请求 例如,在不建立新连接的情况下,保持与页面的连接打开并grep最新数据 此页的大小约为900-1100字节 HTTP获取代码: public static String makeHttpGetRequest(String stringUrl) {
httpget
请求最新页面,当收到数据时,我发送另一个请求
在我当前的实现中,每个请求的时间大约为100-120毫秒。是否有可能使这更快,因为它是相同的url请求
例如,在不建立新连接的情况下,保持与页面的连接打开并grep最新数据
此页的大小约为900-1100字节
HTTP获取代码:
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;
}
读取输入流
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();
}
正如我所知,没有像您所要求的那样的实现。我已经处理了很多http请求,您可以做的最好的事情就是编写代码。还有一件事需要注意…您的连接可能会变慢,并且取决于连接时间可能会更长,或者在某些情况下,我一直在处理连接超时不够大,但这是服务器问题 我认为你应该利用你现在拥有的