Android Http Post / Get java.net.SocketException:操作超时

时间:2012-05-18 08:11:26

标签: android post get timeout

我正在使用Http Post / Get方法向服务器发送数据。

但经常会出现此错误:java.net.SocketException:操作超时

在我看到互联网的那一刻也起作用。

我的代码:

HttpPost request = new HttpPost(url);

            //add headers
            for(NameValuePair h : headers)
            {
                request.addHeader(h.getName(), h.getValue());
            }

            HttpParams httpParameters = new BasicHttpParams();

    HttpConnectionParams.setConnectionTimeout(httpParameters, 10000); 
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);
    HttpConnectionParams.setTcpNoDelay(httpParameters, true);

    HttpClient client = new DefaultHttpClient(httpParameters);        

    HttpResponse httpResponse;

    try {
        httpResponse = client.execute(request);

        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            instream.close();
        }

    } catch (ClientProtocolException e)  {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    }

如果你帮忙,我将不胜感激。

感谢。

0 个答案:

没有答案