Android - HttpPost抛出SocketException:Socket关闭

时间:2013-09-05 16:57:44

标签: android http-post socketexception

我有一个简单的登录表单,它只使用HttpPost连接到服务器并接收一些数据。它在模拟器上运行正常,但在设备上会抛出 java.net.SocketException: Socket closed 。我希望对可能出现的问题有任何帮助。

  • 如果我使用 HttpGet (并更改服务器端以接收GET),它可以正常工作。
  • INTERNET 权限包含在清单
  • url & nameValuePairs doInBackground 方法(AsyncTask)传递
  • url没有问题(在预期的浏览器或模拟器中工作)
  • localhost
  • 模拟器和设备都使用API​​ 10

此行导致问题: HttpResponse response = httpclient.execute(httppost);

  • 设备是否可能以某种方式导致问题? (尚未在其他设备上测试过)
  • org.apache.http.client.methods.HttpPost 是否可以替代发布数据?

如果需要更多信息,请与我们联系。 谢谢你的时间

方法:

static HttpClient httpclient = new DefaultHttpClient();

public static String postRequest(String url, List<NameValuePair> nameValuePairs)
{
    String resp = "";
    HttpPost httppost = new HttpPost(url);

    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        resp = IS2S.inputStreamToString(response.getEntity().getContent());
    }
    catch (ClientProtocolException e) {}
    catch (IOException e) {
        Log.e("666---" , e.getMessage());
           e.printStackTrace();
    }
    return resp;
}

堆栈跟踪:

java.net.SocketException: Socket closed
    at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
    at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:273)
    at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:458)
    at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:421)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490)

1 个答案:

答案 0 :(得分:0)

对每封邮件使用flush()而不关闭。 并且也只使用一个输出流。