SocketException:recvfromfailed:ECONNRESET是什么意思

 我来答
xiangjuan314
2016-04-18 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2889万
展开全部
package com.ucaimalls.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.util.Log;

/**
* 网络连接类
*/
public class HttpHelper
{
public static final int TIME_OUT_MILL = 10000;// 超时毫秒数
/**
* HttpURLConnection 请求方式,直接返回字符串数据
*/
public static String getHttpUrlConnData(String url) throws Exception
{

URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(TIME_OUT_MILL);
conn.setReadTimeout(TIME_OUT_MILL);
InputStream is = conn.getInputStream();
ByteArrayOutputStream dis = new ByteArrayOutputStream();
int realRead = 0;
byte[] buff = new byte[1024];
while ((realRead = is.read(buff )) != -1)
{
dis.write(buff,0,realRead);
}
String data = new String(dis.toByteArray(), "UTF-8").trim();
// System.out.println(data);
if (dis != null)
{
dis.close();
}
if(conn!=null)
{
conn.disconnect();//断开连接---问题产生的原因了
}

return data;

}

}
ok, the answer was that it's the server's fault - it had to close the connection after each request .
it might be that android keeps a pool of connections and use the old one or something like that .
anyway , now it works.
EDIT: according to the API of HttpURLConnection , this can be solved on the client side too:
The input and output streams returned by this class are not buffered. Most callers should wrap the returned streams with BufferedInputStream or BufferedOutputStream. Callers that do only bulk reads or writes may omit buffering. When transferring large amounts of data to or from a server, use streams to limit how much data is in memory at once. Unless you need the entire body to be in memory at once, process it as a stream (rather than storing the complete body as a single byte array or string).
To reduce latency, this class may reuse the same underlying Socket for multiple request/response pairs. As a result, HTTP connections may be held open longer than necessary. Calls to disconnect() may return the socket to a pool of connected sockets. This behavior can be disabled by setting the http.keepAlive system property to false before issuing any HTTP requests. The http.maxConnections property may be used to control how many idle connections to each server will be held.
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式