我有一个简单的登录表单,它只使用HttpPost连接到服务器并接收一些数据。它在模拟器上运行正常,但在设备上会抛出 java.net.SocketException: Socket closed
。我希望对可能出现的问题有任何帮助。
HttpGet
(并更改服务器端以接收GET),它可以正常工作。INTERNET
权限包含在清单url
& nameValuePairs
从 doInBackground
方法(AsyncTask)传递此行导致问题: 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)
答案 0 :(得分:0)
对每封邮件使用flush()
而不关闭。
并且也只使用一个输出流。