Android Studio Intermittent HttpsURLConnection失败

时间:2017-11-18 06:46:13

标签: android android-studio httpurlconnection

我有多个应用程序使用相同的代码对服务器进行HttpsURLConnection。偶尔我无法与设备进行任何通信,但是将同一个HttpsURLConnection代码运行到同一服务器地址的其他设备没有问题。

有时第一个设备会在多次尝试之后进行连接。但是,重置设备上的wifi连接始终会立即修复它。

这让我相信连接到服务器的路由器正在从路由表中删除设备,但我希望在代码上额外看一眼。

我有一个单独的活动来使用以下代码制作HttpsURLConnection:

class WebRequest {

static String response = null;
public final static int GET = 1;
private final static int POST = 2;

//Constructor with no parameter
public WebRequest() {

}

/**
 * Making web service call
 *
 * @url - url to make request
 * @requestmethod - http request method
 */
public String makeWebServiceCall(String url, int requestmethod) {
    return this.makeWebServiceCall(url, requestmethod, null);
}

/**
 * Making service call
 *
 * @url - url to make request
 * @requestmethod - http request method
 * @params - http request params
 */
private String makeWebServiceCall(String urladdress, int requestmethod,
                                  HashMap<String, String> params) {
    URL url;
    String response = "";
    try {
        url = new URL(urladdress);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setDoInput(true);

        if (requestmethod == POST) {
            conn.setRequestMethod("POST");
        } else if (requestmethod == GET) {
            conn.setRequestMethod("GET");
        }

        if (params != null) {
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));

            StringBuilder result = new StringBuilder();
            boolean first = true;
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (first)
                    first = false;
                else
                    result.append("&");

                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                result.append("=");
                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            }

            writer.write(result.toString());

            writer.flush();
            writer.close();
            os.close();
        }

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
        } else {
            response = "";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

}

我在Asynctask中使用它调用来自另一个活动的代码:

WebRequest webreq = new WebRequest();

request = webreq.makeWebServiceCall(URL, WebRequest.GET);

我担心代码中的某些内容会导致该设备中出现间歇性连接问题。我注意到的一件事是没有connection.disconnect。

感谢您对此问题的任何见解!

1 个答案:

答案 0 :(得分:1)

经过数月的调试和测试后,我发现了问题.....在休息室里微波炉。

是的; wifi掉线看似随机的特性是由于天线固定在墙壁另一侧的微波干扰造成的。