android SocketException:Socket关闭

时间:2013-12-14 10:33:30

标签: java android sockets

我正在使用DefaultHttpClient对apache服务器执行post请求。 我想得到回应。 当响应像100符号工作正常时,当我有300个符号的biger响应时,我有套接字执行:套接字关闭。

例外

12-14 11:23:26.905: W/System.err(10082): java.net.SocketException: Socket closed
12-14 11:23:26.905: W/System.err(10082):    at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
12-14 11:23:26.905: W/System.err(10082):    at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:273)
12-14 11:23:26.905: W/System.err(10082):    at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:458)
12-14 11:23:26.905: W/System.err(10082):    at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
12-14 11:23:26.905: W/System.err(10082):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
12-14 11:23:26.905: W/System.err(10082):    at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
12-14 11:23:26.905: W/System.err(10082):    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)
12-14 11:23:26.905: W/System.err(10082):    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)
12-14 11:23:26.905: W/System.err(10082):    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:178)
12-14 11:23:26.913: W/System.err(10082):    at com.androidhive.pushnotifications.JSONParser.getJSONFromUrl(JSONParser.java:174)
12-14 11:23:26.913: W/System.err(10082):    at com.androidhive.pushnotifications.JSONParser.doInBackground(JSONParser.java:233)
12-14 11:23:26.913: W/System.err(10082):    at com.androidhive.pushnotifications.JSONParser.doInBackground(JSONParser.java:1)
12-14 11:23:26.913: W/System.err(10082):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-14 11:23:26.913: W/System.err(10082):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-14 11:23:26.913: W/System.err(10082):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-14 11:23:26.921: W/System.err(10082):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-14 11:23:26.921: W/System.err(10082):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-14 11:23:26.921: W/System.err(10082):    at java.lang.Thread.run(Thread.java:1019)

我的代码

DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse getResponse= null;



        if(json_post!=null){
            StringEntity se=null;
            try {
                se = new StringEntity(json_post.toString());
            } catch (UnsupportedEncodingException e3) {
                // TODO Auto-generated catch block
                e3.printStackTrace();
            }
            //sets the post request as the resulting string
            httpPost.setEntity(se);
        }
        HttpProtocolParams.setUserAgent(httpClient.getParams(), "Mozilla/5.0");try {
            getResponse = httpClient.execute(httpPost);

        } catch (ClientProtocolException e2) {
            statusCode = "ClientProtocolException";
            e2.printStackTrace();
        } catch (IOException e2) {
            statusCode = "IOException";
            e2.printStackTrace();
        } finally {
            httpClient.getConnectionManager().shutdown(); // Close the instance here
        }



        if (getResponse == null) return null;
        statusCode = ""+getResponse.getStatusLine().getStatusCode();
        if (!statusCode.equals( ""+HttpStatus.SC_OK) ) { 
           Log.w(getClass().getSimpleName(), 
              "Error " + statusCode + " for URL " + url); 

           return null;
        }


        HttpEntity getResponseEntity = getResponse.getEntity();

        InputStream inputStream = null;
        try {
            inputStream = getResponseEntity.getContent();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        // Read response into a buffered stream
        int readBytes = 0;
        byte[] sBuffer = new byte[512];
        try {
            while ((readBytes = inputStream.read(sBuffer)) != -1) {
                content.write(sBuffer, 0, readBytes);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // Return result from buffered stream
        String dataAsString = new String(content.toByteArray());

请帮助:)

1 个答案:

答案 0 :(得分:0)

问题:我终于“关机”连接,没有这条线工作完美!

finally {
   httpClient.getConnectionManager().shutdown(); // Close the instance 
}