Is there any way to set time out in android, if there is no response from server for specific period.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Following is code that i used for time out
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();
Put this in a try catch block and if exceeds the time it will throw the exception
回答2:
Try checking the coonectivity to the url first before seeking data reply