gpt4 book ai didi

android - 如何在android中设置超时

转载 作者:行者123 更新时间:2023-11-29 02:15:31 28 4
gpt4 key购买 nike

如果服务器在特定时间段内没有响应,是否有任何方法可以在 android 中设置超时。

最佳答案

以下是我用来超时的代码

   uri = new URI(url);
HttpGet method = new HttpGet(uri);
method.addHeader("Content-Type", "application/json");
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 60000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 65000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(method);
HttpEntity responseEntity = response.getEntity();
statuscode = response.getStatusLine().getStatusCode();

把它放在一个try catch block 中,如果超过时间就会抛出异常

关于android - 如何在android中设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4274365/

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