如何在android中设置超时

时间:2010-11-25 06:39:29

标签: android timer

如果在特定时间段内服务器没有响应,有没有办法在android中设置超时。

2 个答案:

答案 0 :(得分:1)

以下是我用于超时的代码

   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块中,如果超过它将抛出异常的时间

答案 1 :(得分:0)

在寻求数据回复之前,请先尝试检查与网址的连接