title: Android Error: java.net.SocketException: Socket closed
body: <p>I'm seeing this error come up in my crash logs hundreds of times a week but I have spent at this point several weeks trying to chase down the error without any success.  I have been unable to reproduce it on any of my devices.  Here's the stack trace:</p>
  
  <pre><code>Posix.java:-2 in "libcore.io.Posix.recvfromBytes"
  Posix.java:131 in "libcore.io.Posix.recvfrom"
  BlockGuardOs.java:164 in "libcore.io.BlockGuardOs.recvfrom"
  IoBridge.java:513 in "libcore.io.IoBridge.recvfrom"
  PlainSocketImpl.java:489 in "java.net.PlainSocketImpl.read"
  PlainSocketImpl.java:46 in "java.net.PlainSocketImpl.access$000"
  PlainSocketImpl.java:241 in "java.net.PlainSocketImpl$PlainSocketInputStream.read"
  AbstractSessionInputBuffer.java:103 in "org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer"
  AbstractSessionInputBuffer.java:191 in "org.apache.http.impl.io.AbstractSessionInputBuffer.readLine"
  DefaultResponseParser.java:82 in "org.apache.http.impl.conn.DefaultResponseParser.parseHead"
  AbstractMessageParser.java:174 in "org.apache.http.impl.io.AbstractMessageParser.parse"
  AbstractHttpClientConnection.java:180 in "org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader"
  DefaultClientConnection.java:235 in "org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader"
  AbstractClientConnAdapter.java:259 in "org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader"
  HttpRequestExecutor.java:279 in "org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse"
  HttpRequestExecutor.java:121 in "org.apache.http.protocol.HttpRequestExecutor.execute"
  DefaultRequestDirector.java:428 in "org.apache.http.impl.client.DefaultRequestDirector.execute"
  AbstractHttpClient.java:555 in "org.apache.http.impl.client.AbstractHttpClient.execute"
  AbstractHttpClient.java:487 in "org.apache.http.impl.client.AbstractHttpClient.execute"
  AbstractHttpClient.java:465 in "org.apache.http.impl.client.AbstractHttpClient.execute"
  Utilities.java:484 in "com.myapp.android.Utilities$8.run"
  </code></pre>
  
  <p>Here's the block of code where the error is coming from... the exact where the crash occurs is <code>HttpResponse response = httpclient.execute(httppost);</code>:</p>
  
  <pre><code> public static HttpPost postData(String URL, final List&lt;NameValuePair&gt; params, final Handler handler) {
          // Create a new HttpClient and Post Header
          //android.util.Log.d("Utilities", "Called postData");
          final HttpClient httpclient = new DefaultHttpClient();
          //httpclient.
          final HttpPost httppost = new HttpPost(URL);
          final Message msg = new Message();
          final Bundle dataBundle = new Bundle();
          final ByteArrayOutputStream out = new ByteArrayOutputStream();
  
          new Thread(){
              @Override
              public void run(){
                  String error = "";
                  String data = "";
                  try {
                      httppost.setEntity(new UrlEncodedFormEntity(params));
                      HttpResponse response = httpclient.execute(httppost);
                      StatusLine statusLine = response.getStatusLine();
                      if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                          response.getEntity().writeTo(out);
                          out.close();
                          data = out.toString();
                      } else{
                          error = EntityUtils.toString(response.getEntity());
                      }
                  } catch (ClientProtocolException e) {
                      AirbrakeNotifier.notify(e);
                      error = e.toString();
                  } catch (IOException e) {
                      AirbrakeNotifier.notify(e);
                      error = e.toString();
                  } catch (Exception ex) {
                      AirbrakeNotifier.notify(ex);
                      error = ex.toString();
                  }
                  dataBundle.putString("error", error);
                  dataBundle.putString("data", data);
                  msg.setData(dataBundle);
                  handler.dispatchMessage(msg);
              }
          }.start();
          return httppost;
      }
  </code></pre>
  
  <p>Any help on finally figuring this one out is greatly appreciated!</p>