0

贝娄是我调用服务的代码,但它给出了超时异常

    **DefaultHttpClient httpC = new DefaultHttpClient();
    HttpProtocolParams.setUseExpectContinue(httpC.getParams(), false);
    HttpConnectionParams.setConnectionTimeout(httpC.getParams(), 15000);
    HttpConnectionParams.setSoTimeout(httpC.getParams(), 15000);
    HttpPost httpPost = new HttpPost(WebServiceDetails.URL_WS_USER);
    httpPost.setHeader("SOAPAction", soapAction);


    try {
        se = new StringEntity(serviceXml,HTTP.UTF_8);
        se.setContentType("text/xml");
        httpPost.setEntity(se);
        BasicHttpResponse basicHttpResponse =   (BasicHttpResponse)httpC.execute(httpPost);
        HttpEntity responseEntity = basicHttpResponse.getEntity();
        InputStream inputStream = null;
        inputStream = responseEntity.getContent();
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream),1024*5);
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }
        System.out.println("Response from service -"+total.toString());
        xmlContent=total.toString();
        System.out.println(" status Line = "+basicHttpResponse.getStatusLine());


    }catch (Exception e){e.printStackTrace();}**

它可以在模拟器上运行,但不能在实际设备上运行,如果错误,请提出任何教程或示例.......

谢谢x...

4

1 回答 1

0

您的真实设备可能会通过移动网络遇到缓慢的 Internet 连接速度。

为了克服超时问题,我建议您简单地增加超时时间,以便为您的网络套接字购买更多时间来完成它们的操作。例如:

HttpConnectionParams.setConnectionTimeout(httpC.getParams(), 40000); //40 seconds
HttpConnectionParams.setSoTimeout(httpC.getParams(), 40000);         //40 seconds
于 2012-09-24T14:24:00.337 回答