2

我的 Android 应用程序正在使用 SSL 和 GET 请求与网络服务器通信。切换到 API 10 (Gingerbread) 后,SSL 连接工作 - 但仅在应用程序启动后第一次......

第一个请求由主 Activity 发送 - 得到响应后,另一个 Activity 启动并发送多个请求。他们都没有回答。在这两种情况下,请求都是使用在新 AsyncTask 中启动的小 WebService 类发送的。在缩小这个 alas 之后,它实际包含的唯一内容是 URL(-String)。每个活动都启动它自己的此类实例。

这是应该执行 GET 请求的方法。正如很容易看到的那样,我包含了一些代码来避免保持活动 - 并不是我不喜欢它,但在其他答案中建议这样做以避免多个连接出现问题。好吧,在我的情况下它不起作用。

public String webGet(String methodName, Map<String, String> params) {
    String getUrl = webServiceUrl + methodName;
    index++;
    final int connectionID = index;

    int i = 0;
    for (Map.Entry<String, String> param : params.entrySet()) {
        if (i == 0) {
            getUrl += "?";
        } else {
            getUrl += "&";
        }

        try {
            getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        i++;
    }

    String response;
    Log.e("WebGetURL", "["+connectionID+"] " + getUrl);
    URL url;
    try {
        url = new URL(getUrl);
    } catch (MalformedURLException e) {
        Log.e("WebService", "Malformed URL: " + getUrl);
        return null;
    }
    HttpURLConnection urlConnection;
    try {
        Log.e("WebGetResponse", "["+connectionID+"] openConnection()");
        System.setProperty("http.keepAlive", "false");
        if (webServiceSsl) {
            Log.e("WebService", "Using HTTPS");
            urlConnection = (HttpsURLConnection) url.openConnection();
        } else {
            urlConnection = (HttpURLConnection) url.openConnection();
        }
        urlConnection.setUseCaches(false); 
        urlConnection.setRequestProperty("Connection","Keep-Alive"); 
        urlConnection.setDoOutput(false); 
        urlConnection.setDoInput(true); 
        urlConnection.setRequestMethod("GET");
    } catch (IOException e) {
        Log.e("WebService", "I/O exception opening connection: " + e.getMessage());
        e.printStackTrace();
        return null;
    }

    urlConnection.setConnectTimeout(5000);
    urlConnection.setReadTimeout(10000);
    urlConnection.setRequestProperty("Connection", "close");

    try {
        urlConnection.connect();
        Log.e("WebGetResponse", "["+connectionID+"] getInputStream()");
        // This is the last thing I hear from my thread
        BufferedInputStream bin = new BufferedInputStream(urlConnection.getInputStream());
        Log.e("WebGetResponse", "["+connectionID+"] gotInputStream()");
        byte[] contents = new byte[1024];

        int bytesRead=0;
        StringBuilder strFileContents = new StringBuilder();
        Log.e("WebGetResponse", "["+connectionID+"] Waiting for data");
        while((bytesRead = bin.read(contents)) != -1) {
            String add = new String(contents, 0, bytesRead);
            strFileContents.append(add);
        }
        bin.close();
        response = strFileContents.toString();
    } catch (IOException e) {
        Log.e("WebService", "I/O exception reading stream: " + e.getMessage());
        e.printStackTrace();
        return null;
    } finally {
        urlConnection.disconnect();
    }

    Log.e("WebGetResponse", "["+connectionID+"] " + response);
    return response;
}

我一直在尝试搜索 - 我不明白这个问题。实际上我目前无法在非 https 服务器上测试该类,所以我不知道问题是否也出现在 HTTP 中。但是,握手似乎有效,因为第一个请求效果很好。

这是应该启动请求的代码(最终参数是要发送的 GET 内容):

    class ServerDataThread extends AsyncTask<Integer, Integer, String[]> {

        @Override
        protected String[] doInBackground(Integer... attempts) {
            sendActive++;
            int count = attempts.length;
            String[] responses = new String[count];
            for (int i = 0; i < count; i++) {
                responses[i] = server.webGet("collector.php", params);
            }
            return responses;
        }

        protected void onPostExecute(String[] responses) {
            sendActive--;
            for (int i = 0; i < responses.length; i++) {
                if (responses[i] == null) {
                    continue;
                }
                onResponseData(responses[i]);
            }
        }
    }
    new ServerDataThread().execute(0); 

谁能帮助我提示我做错了什么?非常感谢!

BurninLeo

4

0 回答 0