2013-12-14 150 views
1

我正在用DefaultHttpClient执行对Apache服务器的post请求。 我想获得回应。 当响应像100个符号工作正常,byt当我有300个符号的响应时间我有套接字豁免:套接字关闭。android SocketException:套接字关闭

例外

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

请帮助:)

+0

哪一行是174? – EJP

+0

this one - > content.write(sBuffer,0,readBytes); – user1309635

+0

废话。 174行调用read()。看看堆栈跟踪。 content.write(...)甚至不使用套接字。为什么它会抛出'套接字关闭'异常? – EJP

回答

0

的问题:我是在最后 “关机” conecction不用其他这一行正在完善!

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

是的,那行代码是非常不成熟的。它应该在方法的最后。道德:不要编写顺序的“尝试”块:如果必须,请写*嵌套*'尝试'块。 – EJP