Android 網(wǎng)絡(luò)請求框架解析之okhttp與okio
安卓網(wǎng)絡(luò)請求
先看一下今天的大綱
- 導(dǎo)入okhttp和okio依賴
- 禁用掉明文流量請求的檢查
- 添加訪問權(quán)限
- 布局及代碼實(shí)現(xiàn)
- 運(yùn)行結(jié)果

下面是具體步驟
一、導(dǎo)入okhttp和okio的依賴
1.打開File-Project Structure-Dependencies,

2.選擇自己的程序文件,點(diǎn)擊加號,選擇Library Dependency

3.搜索okhttp,選擇Com.squareup.okhttp3,點(diǎn)擊ok按鈕,此時(shí)可能需要較長時(shí)間

4.okio同上

5.應(yīng)用,確認(rèn)

6.此時(shí)我們可以看到Gradle Scripts-build.gradle (Module: My_Application.app)多了兩個(gè)依賴
Module: My_Application.app是自己對應(yīng)的app

二、禁用掉明文流量請求的檢查
1.在res目錄下新建xml文件夾,在xml文件夾下新建nettools.xml
nettools.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!--禁用掉明文流量請求的檢查-->
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

2.在manifests-AndroidManifest.xml中添加剛才創(chuàng)建的nettools.xml
android:networkSecurityConfig="@xml/nettools"

三、添加網(wǎng)絡(luò)請求權(quán)限
在manifests-AndroidManifest.xml中添加
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />```

四、代碼實(shí)現(xiàn)
1.主代碼的實(shí)現(xiàn)
MainActivity.java
import androidx.annotation.UiThread;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private Button btn;
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn = findViewById(R.id.btn);
txt = findViewById(R.id.txt);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request();
}
});
}
protected void request() {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.baidu.com")
.build();
Response response = null;
String string = null;
try {
response = client.newCall(request).execute();
string = response.body().string();
} catch (
IOException e) {
e.printStackTrace();
}
String finalString = string;
runOnUiThread(new Runnable() {
@Override
public void run() {
txt.setText(finalString);
}
});
}
}).start();
}
}
2.主布局的實(shí)現(xiàn)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
五、運(yùn)行結(jié)果
如果運(yùn)行失敗可能是模擬器的問題,建議換模擬器或直接用真機(jī)

到此這篇關(guān)于Android 網(wǎng)絡(luò)請求框架解析之okhttp與okio的文章就介紹到這了,更多相關(guān)Android okhttp 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義View——扇形統(tǒng)計(jì)圖的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android自定義View——扇形統(tǒng)計(jì)圖的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Flutter 使用Navigator進(jìn)行局部跳轉(zhuǎn)頁面的方法
這篇文章主要介紹了Flutter 使用Navigator進(jìn)行局部跳轉(zhuǎn)頁面的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
關(guān)于android studio通過命令行運(yùn)行g(shù)radle編譯命令的問題
這篇文章主要介紹了關(guān)于android studio通過命令行運(yùn)行g(shù)radle編譯命令的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
Android 實(shí)現(xiàn)會旋轉(zhuǎn)的餅狀統(tǒng)計(jì)圖實(shí)例代碼
這篇文章主要介紹了Android 實(shí)現(xiàn)會旋轉(zhuǎn)的餅狀統(tǒng)計(jì)圖實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12
Android手勢滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小
這篇文章主要為大家詳細(xì)介紹了Android手勢滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決
這篇文章主要為大家介紹了Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Android日期選擇器實(shí)現(xiàn)年月日三級聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android日期選擇器實(shí)現(xiàn)年月日三級聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android 輕松實(shí)現(xiàn)語音識別詳解及實(shí)例代碼
這篇文章主要介紹了Android 輕松實(shí)現(xiàn)語音識別的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-09-09

