HttpResponse&适用于Android的超时

时间:2012-11-06 14:51:25

标签: java android timeout httpresponse

我希望我的设备在5秒后放弃http连接。 但是我的代码不起作用......当关闭网络时,我从未收到任何超时消息。 就像设备仍然尝试连接一样,尽管de timeout ......

有个主意吗? 我想抓住正确的例外吗?

感谢。

        try 
        {
            HttpClient httpclient = new DefaultHttpClient();       
                HttpPost httppost = new HttpPost(URL);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpParams httpParameters = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);

                HttpResponse response = httpclient.execute(httppost);

                if (response.getStatusLine().getStatusCode() < 400) 
                {
                        ... //data processing
                } 
                else 
                {
                    errorMsgId = R.string.http_site_error;                        
                }
        } 
        catch (ConnectTimeoutException e)
        {
            Toast.makeText(this, "Network timeout reached!", Toast.LENGTH_SHORT).show();
            Log.e("+++++++++++++++++ ","Network timeout reached!"); 
        }

3 个答案:

答案 0 :(得分:3)

好的,GOT IT,以防万一这可以帮助其他人:

                    HttpClient httpclient = new DefaultHttpClient();
                    final HttpParams httpParams = httpclient.getParams();
                    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
                    HttpConnectionParams.setSoTimeout(httpParams, 5000);
                    HttpPost httppost = new HttpPost(URL);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
                    HttpResponse response = httpclient.execute(httppost);

答案 1 :(得分:1)

您可以使用以下内容:

/**
 * Check availability of web service
 * 
 * @param host Address of host
 * @param seconds Timeout in seconds
 * @return Availability of host
 */
public static boolean checkIfURLExists(String host, int seconds)
{
    HttpURLConnection httpUrlConn;
    try
    {
        httpUrlConn = (HttpURLConnection) new URL(host).openConnection();

        // Set timeouts in milliseconds
        httpUrlConn.setConnectTimeout(seconds * 1000);
        httpUrlConn.setReadTimeout(seconds * 1000);

        // Print HTTP status code/message for your information.
        System.out.println("Response Code: " + httpUrlConn.getResponseCode());
        System.out.println("Response Message: "
                + httpUrlConn.getResponseMessage());

        return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e)
    {
        System.out.println("Error: " + e.getMessage());
        return false;
    }
}

答案 2 :(得分:0)

也许我错过了什么,但是你在哪里将你设置超时的参数与你创建的HttpClient相关联?你不应该这样做:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
...
HttpClient httpclient = new DefaultHttpClient(httpParameters);