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

时间:2015-07-22 10:37:28

标签: java android sockets httpclient

我正在挖掘很长一段时间,我想知道如何在Java(Android)中打开HttpClient连接,然后在检查网络监视工具时立即关闭套接字而不获取CLOSE_WAIT和TIME_WAIT TCP状态

我正在做的是(在stackoverflow站点上找到此解决方案):

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或/和TIME_WAIT约1分钟,然后才会进入CLOSED状态。

问题是: 我在无限循环中每隔~2到5秒调用上面的函数,这导致了大量的CLOSE_WAIT和TIME_WAITs - 这会影响我的Android应用程序的整体性能,直到它被卡住并且无用!

我想做的是(如果可能,还需要你的回答): 我希望在没有任何打开套接字的情况下转发响应消息后关闭连接。没有TIME_WAIT而没有CLOSE_WAIT。没有任何遗留问题 - 在我运行应该执行此操作的代码的瞬间关闭所有通信立即关闭。在循环的下一次迭代之前,我不再需要连接。

我怎样才能做到这一点? 我记得我不希望应用程序停止运行或性能不佳,因为它应该在服务中运行/永远保持打开状态。

如果您可以编写在复制粘贴后工作的简单代码,我将非常感激。 我是Java和Android的新手,因此我将尝试找出您编写的代码,因此请尽量保持简单。非常感谢!

提问者。

2 个答案:

答案 0 :(得分:0)

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();
  }

答案 1 :(得分:0)

尝试使用HttpURLConnection类。请参考以下链接: 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();
}
相关问题