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



你可能感兴趣的:(android)