How to set time out in android

2019-04-12 14:55发布

Is there any way to set time out in android, if there is no response from server for specific period.

标签: android timer
2条回答
时光不老,我们不散
2楼-- · 2019-04-12 15:19

Try checking the coonectivity to the url first before seeking data reply

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-12 15:27

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

查看更多
登录 后发表回答