警告!!!套接字异常:套接字关闭

时间:2014-04-24 11:59:29

标签: android sockets

我从asyntask中的json检索一些数据,然后转到另一个页面而不在json中完成检索数据。发生套接字异常(警告)。这是日志猫。

04-24 07:41:45.565: W/SingleClientConnManager(2504): Invalid use of SingleClientConnManager: connection still allocated.
04-24 07:41:45.565: W/SingleClientConnManager(2504): Make sure to release the connection before allocating another one.
04-24 07:41:45.795: W/System.err(2504): java.net.SocketException: Socket closed
04-24 07:41:45.845: W/System.err(2504):     at libcore.io.Posix.recvfromBytes(Native Method)
04-24 07:41:45.845: W/System.err(2504):     at libcore.io.Posix.recvfrom(Posix.java:141)
04-24 07:41:45.845: W/System.err(2504):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
04-24 07:41:45.855: W/System.err(2504):     at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
04-24 07:41:45.855: W/System.err(2504):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
04-24 07:41:45.895: W/System.err(2504):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
04-24 07:41:46.005: W/System.err(2504):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
04-24 07:41:46.005: W/System.err(2504):     at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
04-24 07:41:46.095: W/System.err(2504):     at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
04-24 07:41:46.135: W/System.err(2504):     at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161)
04-24 07:41:46.135: W/System.err(2504):     at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159)
04-24 07:41:46.145: W/System.err(2504):     at java.io.InputStreamReader.read(InputStreamReader.java:233)
04-24 07:41:46.145: W/System.err(2504):     at java.io.BufferedReader.fillBuf(BufferedReader.java:145)
04-24 07:41:46.145: W/System.err(2504):     at java.io.BufferedReader.readLine(BufferedReader.java:397)
04-24 07:41:46.145: W/System.err(2504):     at com.speedlink.common.Global.executeHttpPost(Global.java:76)
04-24 07:41:46.145: W/System.err(2504):     at com.speedlink.common.WheelDataRetriever.doInBackground(WheelDataRetriever.java:46)
04-24 07:41:46.145: W/System.err(2504):     at com.speedlink.common.WheelDataRetriever.doInBackground(WheelDataRetriever.java:1)
04-24 07:41:46.225: W/System.err(2504):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-24 07:41:46.255: W/System.err(2504):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-24 07:41:46.265: W/System.err(2504):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-24 07:41:46.265: W/System.err(2504):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-24 07:41:46.305: W/System.err(2504):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-24 07:41:46.305: W/System.err(2504):     at java.lang.Thread.run(Thread.java:841)

如何解决此警告?

编辑:

       protected String doInBackground(String... arg0) {
     if (isCancelled()){
         cancel(true);
         return "";
      }else{
    try {
        postParameters.add(new BasicNameValuePair("keyCode",Encode.getKeyCode()));
        postParameters.add(new BasicNameValuePair("version",Encode.getVersion()));
        string response = vv.executeHttpPost(vv.common_URL()+"abc.php",postParameters);


    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
      }
}

1 个答案:

答案 0 :(得分:0)

在重新连接另一个请求之前,您需要使用响应正文。您不仅应该读取响应状态,而且应该完全读取响应InputStream到最后一个字节,从而忽略读取的字节。

Use this link for more details