androidhttpclient超時
1. android如何設置連接超時啊
您好,我是這樣設置的,代碼如下:
public static InputStream getHttpByApache(String urlStr) throws Exception {
HttpGet httpGet = new HttpGet(urlStr);
HttpClient httpClient = new DefaultHttpClient();
// 設置連接超時、讀取超時
HttpParams basicHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(basicHttpParams, 3 * 1000);
HttpConnectionParams.setSoTimeout(basicHttpParams, 3 * 1000);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return httpResponse.getEntity().getContent();
}
return null;
}
2. android java怎麼設置超時時間
第一,ConnectionPoolTimeout:
定義了從ConnectionManager管理的連接池中取出連接的超時時間。
出錯會拋出
第二,ConnectionTimeout:
定義了通過網路與伺服器建立連接的超時時間,Httpclient包中通過一個非同步線程去創建與伺服器的socket連接,這就是該socket連接的超時時間。
當連接HTTP伺服器或者等待HttpConnectionManager管理的一個有效連接超時出錯會拋出ConnectionTimeoutException
第三,SocketTimeout:
這定義了Socket讀數據的超時時間,即從伺服器獲取響應數據需要等待的時間。
當讀取或者接收Socket超時會拋出SocketTimeoutException
3. android中http get請求總是超時怎麼辦
檢查網路,查看是否有許可權,httpclient框架實現示例代碼:
1. GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
4. 如何在Android開發中用HttpClient連接網路數據
HttpClient網路訪問
一、HttpClient網路訪問:
(一)、簡介:
1、Apache組織提供了HttpClient項目,可以實現網路訪問。在Android中,成功集成了HttpClient,所以在Android中可以直接使用HttpClient訪問網路。
2、與HttpURLConnection相比,HttpClient將前者中的輸入、輸出流操作,統一封裝成HttpGet、HttpPost、HttpRequest類。
HttpClient:網路連接對象;
HttpGet:代表發送GET請求;
HttpPost:代表發送POST請求;
HttpResponse:代表處理伺服器響應的對象。
HttpEntity對象:該對象中包含了伺服器所有的返回內容。
3、使用步驟:(六部曲)【重點】
創建HttpClient對象:通過實例化DefaultHttpClient獲得;
創建HttpGet或HttpPost對象:通過實例化 HttpGet或HttpPost 獲得,而構造方法的參數是urlstring(即需要訪問的網路url地址)。也可以通過調用setParams()方法來添加請求參數;
調用HttpClient對象的execute()方法,參數是剛才創建的 HttpGet或HttpPost對象 ,返回值是HttpResponse對象;
通過response對象中的getStatusLine()方法和getStatusCode()方法獲取伺服器響應狀態是否是200。
調用 HttpResponse對象的getEntity()方法,返回HttpEntity對象。而該對象中包含了伺服器所有的返回內容。
藉助EntityUtils的toString()方法或toByteArray()對 HttpEntity對象進行處理,也可以通過IO流對 HttpEntity對象進行操作。
(二)、封裝HttpClientHelper工具類:
public class HttpClientHelper {
public static HttpClient checkNetwork(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
return httpClient;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:實現網路訪問文件,將獲取到數據儲存在文件流中
*
* @param url
* :訪問網路的url地址
* @return inputstream
*/
public static InputStream loadFileFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
return entity.getContent();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:實現網路訪問文件,將獲取到的數據存在位元組數組中
*
* @param url
* :訪問網路的url地址
* @return byte[]
*/
public static byte[] loadByteFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("====>" + e.toString());
}
return null;
}
/**
* 作用:實現網路訪問文件,返回字元串
*
* @param url
* :訪問網路的url地址
* @return String
*/
public static String loadTextFromURL(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toString(httpEntity, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:實現網路訪問文件,先給伺服器通過「GET」方式提交數據,再返回相應的數據
*
* @param url
* :訪問網路的url地址
* @param params
* String url:訪問url時,需要傳遞給伺服器的參數。
* 第二個參數格式為:username=wangxiangjun&password=123456
* @return byte[]
*/
public static byte[] doGetSubmit(String url, String params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet requestGet = new HttpGet(url + "?" + params);
try {
HttpResponse httpResponse = httpClient.execute(requestGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:實現網路訪問文件,先給伺服器通過「POST」方式提交數據,再返回相應的數據
*
* @param url
* :訪問網路的url地址
* @param params
* String url:訪問url時,需要傳遞給伺服器的參數。 第二個參數為:List<NameValuePair>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, List<NameValuePair> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
try {
requestPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 作用:實現網路訪問文件,先給伺服器通過「POST」方式提交數據,再返回相應的數據
*
* @param url
* :訪問網路的url地址
* @param params
* String url:訪問url時,需要傳遞給伺服器的參數。 Map<String , Object>
* @return byte[]
*/
public static byte[] doPostSubmit(String url, Map<String, Object> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost requestPost = new HttpPost(url);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
try {
if (params != null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
BasicNameValuePair nameValuePair = new BasicNameValuePair(
key, value);
parameters.add(nameValuePair);
}
}
requestPost
.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
HttpResponse httpResponse = httpClient.execute(requestPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}