在Android 5.0.2上使用HttpURLConnection时无响应(assus ZenPad 8.0 z380KL)

时间:2015-12-22 09:23:48

标签: android httpurlconnection

我面临的问题是,仅通过移动数据在Android 5.0.2 assus zenpad 8平板电脑上获取asp.net webservice的响应。

所有接缝都能正常工作,直到超过50秒/ 60秒才能延迟响应。

其他平板电脑和手机模型接缝工作到现在为止我不知道是什么导致了这种行为。

public String test(int delayResponse) {
    URL url;
    HttpURLConnection connection = null;
    try {
        url = new URL("http://192.168.1.10/Test.asmx");

        String requestMessage = getRequest(delayResponse);

        connection = (HttpURLConnection) url.openConnection();


        Log.i("ReadTimeout", Integer.toString(connection.getReadTimeout()));
        connection.setChunkedStreamingMode(0);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8;");
        connection.setRequestProperty("SOAPAction", "http://tempuri.org/TestConectivity");

        connection.setRequestProperty("Content-Length", Integer.toString(requestMessage.getBytes().length));

        connection.setConnectTimeout(1000);
        connection.setReadTimeout((delayResponse + 10)*1000);

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.connect();

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(requestMessage);
        wr.flush();
        wr.close();

        if ( connection.getResponseCode() == 200) {

                StringBuilder sbResponse = new StringBuilder();

                InputStream stream = connection.getInputStream();

                InputStreamReader isReader = new InputStreamReader(stream ); 
                BufferedReader br = new BufferedReader(isReader);
                String line;
                while ((line = br.readLine()) != null) {
                    sbResponse.append(line);
                }
                br.close();
                isReader.close();
                stream.close();
                return sbResponse.toString();
        }
        else {
            return Integer.toString(connection.getResponseCode());
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return e.getMessage();
    } catch (IOException e) {
        e.printStackTrace();
        return e.getMessage() + e.toString();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

private String getRequest(int delayResponse) {
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
    sb.append("<soap:Body>");
    sb.append("<TestConectivity xmlns=\"http://tempuri.org/\">");
    sb.append("<delayResponse>"+Integer.toString(delayResponse)+"</delayResponse>");
    sb.append("</TestConectivity>");
    sb.append("</soap:Body>");
    sb.append("</soap:Envelope>");
    return sb.toString();
}

这是ws代码的片段

/// <summary>
/// Summary description for Test
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Test : System.Web.Services.WebService
{

    [WebMethod]
    public string TestConectivity(int delayResponse)
    {
        System.Threading.Thread.Sleep(delayResponse * 1000);
        return "Hello World";
    }
}

1 个答案:

答案 0 :(得分:0)

看来有些gsm运营商(在我的情况下是Vodafone)通过公共APN连接超时...认为他们的网络专家认为需要优化他们的4G,我很高兴他们的竞争对手没有#39; t