Android Studio OkHttpClient使用教程詳解
本次來(lái)記錄下OkHttpClient的使用,OkHttpClient是用來(lái)完成android 客戶端對(duì)服務(wù)端請(qǐng)求的工具。
首先記住,使用網(wǎng)絡(luò)的時(shí)候一定要加入權(quán)限,加入到AndroidMainfest.xml中
<uses-permission android:name="android.permission.INTERNET" />
在初次使用的時(shí)候會(huì)出現(xiàn)報(bào)錯(cuò)。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ā)送一個(gè)表單請(qǐng)求
* @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 請(qǐng)求
* 發(fā)送一個(gè)string請(qǐng)求
* @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 請(qǐng)求
* 發(fā)送一個(gè)From請(qǐng)求
* @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) //請(qǐng)求參數(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請(qǐng)求
* 發(fā)送一個(gè)From請(qǐng)求
* @throws Exception
*/
public void SendGetFrom() throws Exception {
Request request = new Request.Builder()
.get() //請(qǐng)求參數(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);
}
}
測(cè)試發(fā)現(xiàn),上面的用不了,下面放一個(gè)測(cè)試通過(guò)的方法:
public void getDatasyncFactory(){
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();//創(chuàng)建OkHttpClient對(duì)象
Request request = new Request.Builder()
.url("http://123.207.70.54:8080/SpringMvc/hello")//請(qǐng)求接口。如果需要傳參拼接到接口后面。
.build();//創(chuàng)建Request 對(duì)象
Response response = null;
response = client.newCall(request).execute();//得到Response 對(duì)象
if (response.isSuccessful()) {
Log.d("kwwl","response.code()=="+response.code());
Log.d("kwwl","response.message()=="+response.message());
Log.d("kwwl","res=="+response.body());
//此時(shí)的代碼執(zhí)行在子線程,修改UI的操作請(qǐng)使用handler跳轉(zhuǎn)到UI線程。
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
返回信息:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)簡(jiǎn)易登陸注冊(cè)邏輯的實(shí)例代碼
在android的應(yīng)用中越來(lái)越多的包含了網(wǎng)絡(luò)互動(dòng)功能,這就帶來(lái)了注冊(cè),登陸賬號(hào)功能,這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)簡(jiǎn)易登陸注冊(cè)邏輯的相關(guān)資料,需要的朋友可以參考下2021-06-06
Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android studio配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn)
這篇文章主要介紹了Android studio配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解
本篇文章介紹如何利用Android Studio提供的Live Templates更優(yōu)雅實(shí)現(xiàn)自定義View的構(gòu)造方法,說(shuō)句人話就是:簡(jiǎn)化自定義View構(gòu)造參數(shù)模板代碼的編寫(xiě),實(shí)現(xiàn)自動(dòng)生成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android ListView之setEmptyView正確使用方法
這篇文章主要介紹了Android ListView之setEmptyView正確使用方法的相關(guān)資料,希望通過(guò)本文能幫助到大家使用該方法,需要的朋友可以參考下2017-09-09

