一:概述java
1.說明android
java與android均可以使用。web
是網絡請求的開源框架。spring
square公司開發,用於替代HttpUrlConnection和Apache HttpClient數組
2.優勢緩存
3.功能服務器
4.準備項目網絡
在這裏須要使用兩個項目進行實驗。session
調用的項目:app
須要添加使用okhttp包,使用端口8090.
被調用的項目:
使用端口8080
是一個普通的項目便可。
二:程序GET
1.get的普通使用
被調用程序
package com.jun.web2forokhttp.okhttp; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController public class GetReq { @GetMapping("/ok/getInfo") public Map getInfo(@RequestParam("type") String type){ Map map =new HashMap(); if("1".equals(type)){ map.put("1","aa"); map.put("2","bb"); }else { map.put("3","cc"); map.put("4","dd"); } return map; } }
2.調用程序
package com.jun.web.okhttp; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.io.IOException; public class GetHttp { public static void main(String[] args) { withoutHeader(); } public static void withoutHeader(){ String url="http://localhost:8080/ok/getInfo?type=18"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); Call call = client.newCall(request); try{ Response response = call.execute(); System.out.println("get="+response.body().string()); } catch (IOException e) { e.printStackTrace(); } } }
效果:
3.request的請求頭中添加參數
被調用程序
package com.jun.web2forokhttp.okhttp; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController public class GetReq { /** * 普通的get請求 * @param type * @return */ @GetMapping("/ok/getInfo") public Map getInfo(@RequestParam("type") String type){ Map map =new HashMap(); if("1".equals(type)){ map.put("1","aa"); map.put("2","bb"); }else { map.put("3","cc"); map.put("4","dd"); } return map; } /** * 參數從請求頭中獲取 * @param type * @return */ @GetMapping("/ok/getMoreInfo") public Map getMoreInfo(@RequestParam("type") String type, @RequestHeader("cjtoken") String cjToken){ Map map =new HashMap(); if("1".equals(type)){ map.put("1","aa"); map.put("2","bb"); map.put("cjtoken",cjToken); }else { map.put("3","cc"); map.put("4","dd"); map.put("cjtoken",cjToken); } return map; } }
4.調用程序
package com.jun.web.okhttp; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.io.IOException; public class GetHttp { public static void main(String[] args) { // withoutHeader(); withHeader(); } /** * 沒有請求頭的請求 */ public static void withoutHeader(){ String url="http://localhost:8080/ok/getInfo?type=18"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .header("cjtoken","8765095321") .build(); Call call = client.newCall(request); try{ Response response = call.execute(); System.out.println("get="+response.body().string()); } catch (IOException e) { e.printStackTrace(); } } /** * 有請求頭的請求 */ public static void withHeader(){ String url="http://localhost:8080/ok/getMoreInfo?type=18"; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .header("cjtoken","8765095321") .build(); Call call = client.newCall(request); try{ Response response = call.execute(); System.out.println("get="+response.body().string()); } catch (IOException e) { e.printStackTrace(); } } }
效果:
三:注意點
1.返回值
string()只是其中之一。
使用response.code()獲取返回的狀態碼。若是成功,則是200.
2.header
能夠寫多個header,添加多個值與健。