掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
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ò)請(qǐng)求是必不可少的一部分,而HttpClient是Android提供的一個(gè)用于發(fā)送HTTP請(qǐng)求的類,它繼承自org.apache.http.client.AbstractHttpClient,可以發(fā)送GET、POST等請(qǐng)求,本文將對(duì)HttpClient進(jìn)行詳細(xì)的介紹。
HttpClient是Apache提供的一個(gè)用于發(fā)送HTTP請(qǐng)求的開源庫,它可以發(fā)送各種類型的HTTP請(qǐng)求,如GET、POST、PUT、DELETE等,在Android中,HttpClient被封裝在org.apache.http.client包中,開發(fā)者可以通過引入該包來使用HttpClient。
1、創(chuàng)建HttpClient實(shí)例
在使用HttpClient之前,首先需要?jiǎng)?chuàng)建一個(gè)HttpClient實(shí)例,可以通過以下兩種方式創(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)建請(qǐng)求方法
創(chuàng)建完HttpClient實(shí)例后,需要?jiǎng)?chuàng)建一個(gè)請(qǐng)求方法,可以通過以下方式創(chuàng)建:
(1)創(chuàng)建Get請(qǐng)求:
String url = "http://www.example.com"; HttpGet httpGet = new HttpGet(url);
(2)創(chuàng)建Post請(qǐng)求:
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í)行請(qǐng)求并獲取響應(yīng)結(jié)果
創(chuàng)建完請(qǐng)求方法后,就可以執(zhí)行請(qǐng)求并獲取響應(yīng)結(jié)果了,可以通過以下方式執(zhí)行請(qǐng)求:
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)閉連接,釋放資源,否則可能會(huì)導(dǎo)致內(nèi)存泄漏。
2、HttpClient不支持并發(fā)請(qǐng)求,如果需要并發(fā)請(qǐng)求,可以使用線程池或者異步任務(wù)來實(shí)現(xiàn)。
3、從Android 6.0開始,系統(tǒng)對(duì)網(wǎng)絡(luò)請(qǐng)求進(jìn)行了限制,需要在運(yùn)行時(shí)動(dòng)態(tài)申請(qǐng)權(quán)限,可以使用Android Support Library中的AppCompatActivity和NetworkInfo類來實(shí)現(xiàn)。
4、如果需要使用HTTPS協(xié)議,需要在創(chuàng)建SSLContext時(shí)傳入一個(gè)密鑰庫文件和密碼。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è)置超時(shí)時(shí)間,可以在創(chuàng)建RequestConfig對(duì)象時(shí)設(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對(duì)象時(shí)設(shè)置:Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.1", 8888));,然后將Proxy設(shè)置到RequestConfig對(duì)象中:requestConfig.setProxy(proxy);,最后將RequestConfig設(shè)置到HttpClientBuilder中:builder.setDefaultRequestConfig(requestConfig);,最后將HttpClientBuilder設(shè)置到CloseableHttpClient中:CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流