0

我有一个简单的登录表单,它只使用 HttpPost 连接到服务器并接收一些数据。它在模拟器上运行良好,但在设备上它会抛出java.net.SocketException: Socket closed. 对于可能出现的问题,我将不胜感激。

  • 如果我使用HttpGet(并将服务器端改为接收 GET)它工作正常。
  • INTERNET权限包含在清单中
  • url&nameValuePairsdoInBackground方法(AsyncTask)传递
  • url 没有问题(在浏览器或模拟器中按预期工作)
  • 不是本地主机
  • 模拟器和设备都使用 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)
4

1 回答 1

0

用于flush()每条消息而不关闭。并且只使用一个输出流。

于 2013-10-10T05:59:52.720 回答