gpt4 book ai didi

android如何知道HttpResponse需要多少时间才能做出回应

转载 作者:行者123 更新时间:2023-11-29 18:19:27 24 4
gpt4 key购买 nike

我有一个应用程序,每次连接到互联网并从互联网上获取数据时,我都想找到它需要多少时间?如果需要更多时间,那么我想警告用户“Internet 连接有问题”

所以我怎么知道它需要多少时间。在我提供的应用程序功能下方,我使用了 HttpResponse。请告诉我如何获得它需要多少时间给予回应

 String page = executeHttpGet("http://192.168.1.109/temp/android.php");

private String executeHttpGet(String URL) throws Exception {

BufferedReader bufferedReader = null;
try {
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
"android");
HttpGet request = new HttpGet();
request.setHeader("Content-Type", "text/plain; charset=utf-8");
request.setURI(new URI(URL));
HttpResponse response = client.execute(request);
bufferedReader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));

StringBuffer stringBuffer = new StringBuffer("");
String line = "";

String NL = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + NL);
System.out.print(stringBuffer);
}
bufferedReader.close();
page = stringBuffer.toString();
System.out.println(page + "page");
return page;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
Log.d("BBB", e.toString());
}
}
}
}

谢谢。

最佳答案

..if takes more time then i want to give warning to...

既然您知道最佳时间,您应该得到响应,为什么不在创建连接时指定超时。有关如何设置超时的信息,请参见下面的代码 fragment 。然后您捕获超时错误并通知服务响应时间太长。

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);

ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);

引用:

  1. > http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/
  2. > Http connection timeout on Android not working

关于android如何知道HttpResponse需要多少时间才能做出回应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6326934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com