首页 文章

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

提问于
浏览
0

我正在挖掘很长一段时间,我想知道如何在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
    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();
      }
    
  • 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();
    }
    

相关问题