2021-05-12 14:32:11
Android Studio OkHttpClient使用教學詳解
2020-09-23 12:00:10
本次來記錄下OkHttpClient的使用,OkHttpClient是用來完成android 使用者端對伺服器端請求的工具。
首先記住,使用網路的時候一定要加入許可權,加入到AndroidMainfest.xml中
<uses-permission android:name="android.permission.INTERNET" />
在初次使用的時候會出現報錯。cannot resolve symbol OkHttpClient
這裡需要引入
implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然後重新整理下專案就可以了。
程式碼:
package com.example.administrator.testclient; import com.squareup.*; import java.io.IOException; import okhttp3.FormBody; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class BaseHttpClient { public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); // 01. 定義okhttp private final OkHttpClient client = new OkHttpClient(); public BaseHttpClient(){ //client.connectTimeoutMillis(); } /** * 傳送一個表單請求 * @throws Exception */ public void SendForm() throws Exception { RequestBody formBody = new FormBody.Builder() .add("search", "Jurassic Park") .build(); Request request = new Request.Builder() .url("https://en.wikipedia.org/w/index.php") .post(formBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } /**POST 請求 * 傳送一個string請求 * @throws Exception */ public void SendPostString() throws Exception { String postBody = "" + "Releasesn" + "--------n" + "n" + " * _1.0_ May 6, 2013n" + " * _1.1_ June 15, 2013n" + " * _1.2_ August 11, 2013n"; Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody)) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } /**POST 請求 * 傳送一個From請求 * @throws Exception */ public void SendPostFrom() throws Exception { RequestBody body = new FormBody.Builder() .add("name", "sy")//新增引數體 .add("age", "18") .build(); Request request = new Request.Builder() .post(body) //請求引數 .url("http://123.207.70.54:8080/SpringMvc/hello") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); } /**Get請求 * 傳送一個From請求 * @throws Exception */ public void SendGetFrom() throws Exception { Request request = new Request.Builder() .get() //請求引數 .url("http://123.207.70.54:8080/SpringMvc/hello") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); } }
測試發現,上面的用不了,下面放一個測試通過的方法:
public void getDatasyncFactory(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient();//建立OkHttpClient物件 Request request = new Request.Builder() .url("http://123.207.70.54:8080/SpringMvc/hello")//請求介面。如果需要傳參拼接到介面後面。 .build();//建立Request 物件 Response response = null; response = client.newCall(request).execute();//得到Response 物件 if (response.isSuccessful()) { Log.d("kwwl","response.code()=="+response.code()); Log.d("kwwl","response.message()=="+response.message()); Log.d("kwwl","res=="+response.body()); //此時的程式碼執行在子執行緒,修改UI的操作請使用handler跳轉到UI執行緒。 } } catch (Exception e) { e.printStackTrace(); } } }).start(); }
返回資訊:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章