java.net.SocketException: recvfrom failed: ECONNRESET

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;
		

   	 
	}

}

今天遇到了这么个错误:

09-11 10:02:34.849: W/System.err(17963): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-11 10:02:34.849: W/System.err(17963): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542)
09-11 10:02:34.849: W/System.err(17963): at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
09-11 10:02:34.859: W/System.err(17963): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
09-11 10:02:34.859: W/System.err(17963): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
09-11 10:02:34.859: W/System.err(17963): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
09-11 10:02:34.859: W/System.err(17963): at java.io.InputStream.read(InputStream.java:163)
09-11 10:02:34.869: W/System.err(17963): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
09-11 10:02:34.869: W/System.err(17963): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
09-11 10:02:34.869: W/System.err(17963): at libcore.io.Streams.readAsciiLine(Streams.java:201)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
09-11 10:02:34.869: W/System.err(17963): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
09-11 10:02:34.869: W/System.err(17963): at com.ucaimalls.util.HttpHelper.getHttpUrlConnData(HttpHelper.java:37)
09-11 10:02:34.869: W/System.err(17963): at com.ucaimalls.util.QueryLocationTask.doInBackground(QueryLocationTask.java:49)
09-11 10:02:34.869: W/System.err(17963): at com.ucaimalls.util.QueryLocationTask.doInBackground(QueryLocationTask.java:1)
09-11 10:02:34.869: W/System.err(17963): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-11 10:02:34.869: W/System.err(17963): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-11 10:02:34.869: W/System.err(17963): at java.lang.Thread.run(Thread.java:856)
09-11 10:02:34.879: W/System.err(17963): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.Posix.recvfromBytes(Native Method)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.Posix.recvfrom(Posix.java:131)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
09-11 10:02:34.889: W/System.err(17963): at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
09-11 10:02:34.889: W/System.err(17963): ... 20 more


在网上找了下答案(老外的回答 啊)


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.

taken from: http//developer.android.com/reference/java/net/HttpURLConnection.html



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值