一,編寫(xiě)返回對(duì)象
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括江南網(wǎng)站建設(shè)、江南網(wǎng)站制作、江南網(wǎng)頁(yè)制作以及江南網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,江南網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到江南省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
public class HttpResult { // 響應(yīng)的狀態(tài)碼 private int code; // 響應(yīng)的響應(yīng)體 private String body; get/set… }
二,封裝HttpClient
package cn.xxxxxx.httpclient; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class APIService { private CloseableHttpClient httpClient; public APIService() { // 1 創(chuàng)建HttpClinet,相當(dāng)于打開(kāi)瀏覽器 this.httpClient = HttpClients.createDefault(); } /** * 帶參數(shù)的get請(qǐng)求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doGet(String url, Mapmap) throws Exception { // 聲明URIBuilder URIBuilder uriBuilder = new URIBuilder(url); // 判斷參數(shù)map是否為非空 if (map != null) { // 遍歷參數(shù) for (Map.Entry entry : map.entrySet()) { // 設(shè)置參數(shù) uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); } } // 2 創(chuàng)建httpGet對(duì)象,相當(dāng)于設(shè)置url請(qǐng)求地址 HttpGet httpGet = new HttpGet(uriBuilder.build()); // 3 使用HttpClient執(zhí)行httpGet,相當(dāng)于按回車,發(fā)起請(qǐng)求 CloseableHttpResponse response = this.httpClient.execute(httpGet); // 4 解析結(jié)果,封裝返回對(duì)象httpResult,相當(dāng)于顯示相應(yīng)的結(jié)果 // 狀態(tài)碼 // response.getStatusLine().getStatusCode(); // 響應(yīng)體,字符串,如果response.getEntity()為空,下面這個(gè)代碼會(huì)報(bào)錯(cuò),所以解析之前要做非空的判斷 // EntityUtils.toString(response.getEntity(), "UTF-8"); HttpResult httpResult = null; // 解析數(shù)據(jù)封裝HttpResult if (response.getEntity() != null) { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } // 返回 return httpResult; } /** * 不帶參數(shù)的get請(qǐng)求 * * @param url * @return * @throws Exception */ public HttpResult doGet(String url) throws Exception { HttpResult httpResult = this.doGet(url, null); return httpResult; } /** * 帶參數(shù)的post請(qǐng)求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doPost(String url, Map map) throws Exception { // 聲明httpPost請(qǐng)求 HttpPost httpPost = new HttpPost(url); // 判斷map不為空 if (map != null) { // 聲明存放參數(shù)的List集合 List params = new ArrayList (); // 遍歷map,設(shè)置參數(shù)到list中 for (Map.Entry entry : map.entrySet()) { params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); } // 創(chuàng)建form表單對(duì)象 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8"); // 把表單對(duì)象設(shè)置到httpPost中 httpPost.setEntity(formEntity); } // 使用HttpClient發(fā)起請(qǐng)求,返回response CloseableHttpResponse response = this.httpClient.execute(httpPost); // 解析response封裝返回對(duì)象httpResult HttpResult httpResult = null; if (response.getEntity() != null) { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } // 返回結(jié)果 return httpResult; } /** * 不帶參數(shù)的post請(qǐng)求 * * @param url * @return * @throws Exception */ public HttpResult doPost(String url) throws Exception { HttpResult httpResult = this.doPost(url, null); return httpResult; } /** * 帶參數(shù)的Put請(qǐng)求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doPut(String url, Map map) throws Exception { // 聲明httpPost請(qǐng)求 HttpPut httpPut = new HttpPut(url); // 判斷map不為空 if (map != null) { // 聲明存放參數(shù)的List集合 List params = new ArrayList (); // 遍歷map,設(shè)置參數(shù)到list中 for (Map.Entry entry : map.entrySet()) { params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); } // 創(chuàng)建form表單對(duì)象 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8"); // 把表單對(duì)象設(shè)置到httpPost中 httpPut.setEntity(formEntity); } // 使用HttpClient發(fā)起請(qǐng)求,返回response CloseableHttpResponse response = this.httpClient.execute(httpPut); // 解析response封裝返回對(duì)象httpResult HttpResult httpResult = null; if (response.getEntity() != null) { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } // 返回結(jié)果 return httpResult; } /** * 帶參數(shù)的Delete請(qǐng)求 * * @param url * @param map * @return * @throws Exception */ public HttpResult doDelete(String url, Map map) throws Exception { // 聲明URIBuilder URIBuilder uriBuilder = new URIBuilder(url); // 判斷參數(shù)map是否為非空 if (map != null) { // 遍歷參數(shù) for (Map.Entry entry : map.entrySet()) { // 設(shè)置參數(shù) uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); } } // 2 創(chuàng)建httpGet對(duì)象,相當(dāng)于設(shè)置url請(qǐng)求地址 HttpDelete httpDelete = new HttpDelete(uriBuilder.build()); // 3 使用HttpClient執(zhí)行httpGet,相當(dāng)于按回車,發(fā)起請(qǐng)求 CloseableHttpResponse response = this.httpClient.execute(httpDelete); // 4 解析結(jié)果,封裝返回對(duì)象httpResult,相當(dāng)于顯示相應(yīng)的結(jié)果 // 狀態(tài)碼 // response.getStatusLine().getStatusCode(); // 響應(yīng)體,字符串,如果response.getEntity()為空,下面這個(gè)代碼會(huì)報(bào)錯(cuò),所以解析之前要做非空的判斷 // EntityUtils.toString(response.getEntity(), "UTF-8"); HttpResult httpResult = null; // 解析數(shù)據(jù)封裝HttpResult if (response.getEntity() != null) { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } else { httpResult = new HttpResult(response.getStatusLine().getStatusCode(), ""); } // 返回 return httpResult; } }
三,調(diào)用接口
package cn.xxxxxx.httpclient.test; import java.util.HashMap; import java.util.Map; import org.junit.Before; import org.junit.Test; import cn.itcast.httpclient.APIService; import cn.itcast.httpclient.HttpResult; public class APIServiceTest { private APIService apiService; @Before public void init() { this.apiService = new APIService(); } // 查詢 @Test public void testQueryItemById() throws Exception { // http://manager.aaaaaa.com/rest/item/interface/{id} String url = "http://manager.aaaaaa.com/rest/item/interface/42"; HttpResult httpResult = this.apiService.doGet(url); System.out.println(httpResult.getCode()); System.out.println(httpResult.getBody()); } // 新增 @Test public void testSaveItem() throws Exception { // http://manager.aaaaaa.com/rest/item/interface/{id} String url = "http://manager.aaaaaa.com/rest/item/interface"; Mapmap = new HashMap (); // title=測(cè)試RESTful風(fēng)格的接口&price=1000&num=1&cid=888&status=1 map.put("title", "測(cè)試APIService調(diào)用新增接口"); map.put("price", "1000"); map.put("num", "1"); map.put("cid", "666"); map.put("status", "1"); HttpResult httpResult = this.apiService.doPost(url, map); System.out.println(httpResult.getCode()); System.out.println(httpResult.getBody()); } // 修改 @Test public void testUpdateItem() throws Exception { // http://manager.aaaaaa.com/rest/item/interface/{id} String url = "http://manager.aaaaaa.com/rest/item/interface"; Map map = new HashMap (); // title=測(cè)試RESTful風(fēng)格的接口&price=1000&num=1&cid=888&status=1 map.put("title", "測(cè)試APIService調(diào)用修改接口"); map.put("id", "44"); HttpResult httpResult = this.apiService.doPut(url, map); System.out.println(httpResult.getCode()); System.out.println(httpResult.getBody()); } // 刪除 @Test public void testDeleteItemById() throws Exception { // http://manager.aaaaaa.com/rest/item/interface/{id} String url = "http://manager.aaaaaa.com/rest/item/interface/44"; HttpResult httpResult = this.apiService.doDelete(url, null); System.out.println(httpResult.getCode()); System.out.println(httpResult.getBody()); } }
以上這篇使用HttpClient調(diào)用接口的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。