导航:首页 > 操作系统 > androidhttphelper

androidhttphelper

发布时间:2022-11-17 05:56:29

⑴ SocketException:recvfromfailed: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;

}

}
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 rece 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.

⑵ android怎么import httphelper

customer asked the owner, "How come this guy is $5,000? That's so expensive for

阅读全文

与androidhttphelper相关的资料

热点内容
单片机矩阵键盘显示 浏览:227
约束边缘柱必须加密吗 浏览:308
android学到什么程度 浏览:374
如何加密一个小区 浏览:313
51单片机控制三相控制器 浏览:814
手机上什么解压软件可以强制解压 浏览:785
win7有自带编译器吗 浏览:545
转接器连了没有文件夹 浏览:574
二手开利螺杆压缩机 浏览:315
有php基础学java要多久 浏览:306
程序员税后工资多少可以跳槽 浏览:174
个别网站无法解析服务器的dns地址 浏览:978
安卓手机如何打开rmb文件 浏览:221
新生儿app叫什么 浏览:71
斗鱼加密怎么弄 浏览:767
为什么会加密不可上网 浏览:537
步步高手机编译时间啥意思 浏览:402
程序员复盘app 浏览:162
pdf确定 浏览:542
php连接mysql端口号 浏览:1005