新聞中心
Android的HttpClient詳解

創(chuàng)新互聯(lián)于2013年開始,先為淇縣等服務(wù)建站,淇縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為淇縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
在Android開發(fā)中,網(wǎng)絡(luò)請求是必不可少的一部分,而HttpClient是Android提供的一個用于發(fā)送HTTP請求的類,它繼承自org.apache.http.client.AbstractHttpClient,可以發(fā)送GET、POST等請求,本文將對HttpClient進(jìn)行詳細(xì)的介紹。
HttpClient簡介
HttpClient是Apache提供的一個用于發(fā)送HTTP請求的開源庫,它可以發(fā)送各種類型的HTTP請求,如GET、POST、PUT、DELETE等,在Android中,HttpClient被封裝在org.apache.http.client包中,開發(fā)者可以通過引入該包來使用HttpClient。
HttpClient使用方法
1、創(chuàng)建HttpClient實例
在使用HttpClient之前,首先需要創(chuàng)建一個HttpClient實例,可以通過以下兩種方式創(chuàng)建:
(1)通過默認(rèn)構(gòu)造函數(shù)創(chuàng)建:
DefaultHttpClient httpClient = new DefaultHttpClient();
(2)通過自定義構(gòu)造函數(shù)創(chuàng)建:
CloseableHttpClient httpClient = HttpClients.custom().build();
2、創(chuàng)建請求方法
創(chuàng)建完HttpClient實例后,需要創(chuàng)建一個請求方法,可以通過以下方式創(chuàng)建:
(1)創(chuàng)建Get請求:
String url = "http://www.example.com"; HttpGet httpGet = new HttpGet(url);
(2)創(chuàng)建Post請求:
String url = "http://www.example.com"; Listparams = new ArrayList<>(); params.add(new BasicNameValuePair("key", "value")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity);
3、執(zhí)行請求并獲取響應(yīng)結(jié)果
創(chuàng)建完請求方法后,就可以執(zhí)行請求并獲取響應(yīng)結(jié)果了,可以通過以下方式執(zhí)行請求:
try {
ResponseHandler responseHandler = new ResponseHandler() {
@Override
public String handleResponse(final HttpResponse httpResponse) throws IOException {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
HttpEntity entity = httpResponse.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + statusCode);
}
}
};
String responseBody = httpClient.execute(httpGet, responseHandler);
System.out.println(responseBody);
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.close();
}
注意事項
1、在使用完HttpClient后,需要調(diào)用其close方法關(guān)閉連接,釋放資源,否則可能會導(dǎo)致內(nèi)存泄漏。
2、HttpClient不支持并發(fā)請求,如果需要并發(fā)請求,可以使用線程池或者異步任務(wù)來實現(xiàn)。
3、從Android 6.0開始,系統(tǒng)對網(wǎng)絡(luò)請求進(jìn)行了限制,需要在運(yùn)行時動態(tài)申請權(quán)限,可以使用Android Support Library中的AppCompatActivity和NetworkInfo類來實現(xiàn)。
4、如果需要使用HTTPS協(xié)議,需要在創(chuàng)建SSLContext時傳入一個密鑰庫文件和密碼。SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();,然后將SSLContext設(shè)置到HttpClientBuilder中:builder.setSSLSocketFactory(sslContext.getSocketFactory());,最后將HttpClientBuilder設(shè)置到CloseableHttpClient中:CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslContext.getSocketFactory()).build();。
5、如果需要設(shè)置超時時間,可以在創(chuàng)建RequestConfig對象時設(shè)置:RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).setSocketTimeout(1000).build();,然后將RequestConfig設(shè)置到HttpClientBuilder中:builder.setDefaultRequestConfig(requestConfig);,最后將HttpClientBuilder設(shè)置到CloseableHttpClient中:CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();。
6、如果需要設(shè)置代理服務(wù)器,可以在創(chuàng)建Proxy對象時設(shè)置:Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.1", 8888));,然后將Proxy設(shè)置到RequestConfig對象中:requestConfig.setProxy(proxy);,最后將RequestConfig設(shè)置到HttpClientBuilder中:builder.setDefaultRequestConfig(requestConfig);,最后將HttpClientBuilder設(shè)置到CloseableHttpClient中:CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();。
網(wǎng)站欄目:android的androidhttpClient詳解
轉(zhuǎn)載注明:http://m.fisionsoft.com.cn/article/dhijhjc.html


咨詢
建站咨詢
