導航:首頁 > 操作系統 > 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相關的資料

熱點內容
php是前台還是後台 瀏覽:416
手機小說怎麼解壓成txt格式 瀏覽:877
伺服器的資源是什麼 瀏覽:9
在當前工程中添加新窗體的命令 瀏覽:460
手機如何連接伺服器的遠程桌面 瀏覽:48
復雜命令的實現 瀏覽:330
抖音上的程序員和真正的程序員 瀏覽:300
查看kernel編譯器 瀏覽:279
給plc程序加密 瀏覽:225
python多進程數據共享 瀏覽:847
華為和安卓系統有什麼不一樣 瀏覽:106
python中wb表怎麼列印 瀏覽:297
python如何把字元串賦給數組 瀏覽:229
狄克斯特拉演算法是什麼 瀏覽:675
室內裝飾材料pdf 瀏覽:633
gitbook命令行 瀏覽:1000
啟動zookeeper命令 瀏覽:527
健身館app怎麼樣 瀏覽:314
python可視化項目 瀏覽:442
安卓機怎麼辨別蘋果機真假 瀏覽:711