您好,登錄后才能下訂單哦!
使用Android Studio OkHttpClient的方法?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
首先記住,使用網(wǎng)絡(luò)的時候一定要加入權(quán)限,加入到AndroidMainfest.xml中
<uses-permission android:name="android.permission.INTERNET" />
在初次使用的時候會出現(xiàn)報錯。cannot resolve symbol OkHttpClient
這里需要引入
implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然后刷新下項(xiàng)目就可以了。
代碼:
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(); } /** * 發(fā)送一個表單請求 * @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 請求 * 發(fā)送一個string請求 * @throws Exception */ public void SendPostString() throws Exception { String postBody = "" + "Releases\n" + "--------\n" + "\n" + " * _1.0_ May 6, 2013\n" + " * _1.1_ June 15, 2013\n" + " * _1.2_ August 11, 2013\n"; 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 請求 * 發(fā)送一個From請求 * @throws Exception */ public void SendPostFrom() throws Exception { RequestBody body = new FormBody.Builder() .add("name", "sy")//添加參數(shù)體 .add("age", "18") .build(); Request request = new Request.Builder() .post(body) //請求參數(shù) .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請求 * 發(fā)送一個From請求 * @throws Exception */ public void SendGetFrom() throws Exception { Request request = new Request.Builder() .get() //請求參數(shù) .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); } }
測試發(fā)現(xiàn),上面的用不了,下面放一個測試通過的方法:
public void getDatasyncFactory(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient();//創(chuàng)建OkHttpClient對象 Request request = new Request.Builder() .url("http://123.207.70.54:8080/SpringMvc/hello")//請求接口。如果需要傳參拼接到接口后面。 .build();//創(chuàng)建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()); //此時的代碼執(zhí)行在子線程,修改UI的操作請使用handler跳轉(zhuǎn)到UI線程。 } } catch (Exception e) { e.printStackTrace(); } } }).start(); }
返回信息:
看完上述內(nèi)容,你們掌握使用Android Studio OkHttpClient的方法的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。