如何在 Android 中打开 HttpClient 连接然后立即关闭它

How do I open an HttpClient connection in Android and then close it immediately

我研究了很长时间,我想知道如何在 Java (Android) 中打开 HttpClient 连接,然后立即关闭套接字而不获取 CLOSE_WAIT 和 TIME_WAIT 我检查网络监控工具时的 TCP 状态。

我正在做的是(在 Whosebug 网站上找到了这个解决方案):

String url = "http://example.com/myfile.php";

String result = null;

InputStream is = null;

StringBuilder sb = null;

    try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection" + e.toString());
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result = sb.toString();
        } catch (Exception e) {

        }

        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

在我 运行 这段代码之后 - PHP 文件执行得很好,我得到了对 TOAST 的响应,但是 - 当我用外部网络分析器分析我的移动设备的网络环境时工具 - 我看到连接保持 CLOSE_WAIT or/and TIME_WAIT 大约 1 分钟,然后它们才进入关闭状态。

问题是: 我在无限循环中每隔 2 到 5 秒调用一次上述函数,随着时间的推移会导致大量的 CLOSE_WAITs 和 TIME_WAITs - 这会影响我的 [=38= 的整体性能] 应用程序,直到它卡住并且无用!

我想做的是(如果可能需要你的回答): 我希望在没有任何打开的套接字的情况下 TOAST 响应消息后立即关闭连接。没有 TIME_WAIT 也没有 CLOSE_WAIT。完全没有遗漏 - 在我 运行 代码应该这样做的瞬间立即关闭所有通信。在循环的下一次迭代之前,我不再需要连接。

我怎样才能做到这一点? 我想我不希望应用程序随着时间的推移停止或性能不佳,因为它应该 运行 在 service/stay 中永远打开。

如果您能编写在我复制粘贴后可以运行的简单代码,我将不胜感激。 我是 Java 和 Android 的新手,所以我会尝试理解您编写的代码,因此请尽可能简单。非常感谢!

提问者。

HttpClient httpclient = new HttpClient();

GetMethod httpget = new GetMethod("http://www.myhost.com/");
try {
    httpclient.executeMethod(httpget);
    Reader reader = new InputStreamReader(httpget.getResponseBodyAsStream(),                    httpget.getResponseCharSet()); 
    // consume the response entity
  } finally {
    httpget.releaseConnection();
  }

尝试使用 HttpURLConnection class。参考以下 link : http://android-developers.blogspot.de/2011/09/androids-http-clients.html

在 finally 子句中调用 disconnect。示例代码 ..

try {
    HttpURLConnection locConn = (HttpURLConnection) locurl.openConnection();
                    //URL url = locConn.getURL();
    locConn.setRequestProperty("Authorization", basicAuth);
    locConn.setRequestMethod("GET");
    locConn.setRequestProperty("Content-Type", "application/json");
    locConn.setRequestProperty("X-Myauthtoken", userCredentials);
    retc = locConn.getResponseCode();
    reader = new BufferedReader(new InputStreamReader(locConn.getInputStream()));

    String sessionK = null;
    readVal = reader.readLine();

    if (retc == 200) {


    }

}catch (...)
{
    //handle exception 
}finally {
    //disconnect here
    locConn.disconnect();
}