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

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

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

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

4.okio同上

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

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

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

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

三、添加網(wǎng)絡(luò)請(qǐng)求權(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)行失敗可能是模擬器的問(wèn)題,建議換模擬器或直接用真機(jī)

到此這篇關(guān)于Android 網(wǎng)絡(luò)請(qǐng)求框架解析之okhttp與okio的文章就介紹到這了,更多相關(guān)Android okhttp 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 物理游戲之重力系統(tǒng)開(kāi)發(fā)示例代碼
介紹Android 物理游戲之重力系統(tǒng),這里提供了詳細(xì)的資料整理,并附示例代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-08-08
如何利用Android Studio將moudle變成jar示例詳解
這篇文章主要給大家介紹了關(guān)于如何利用Android Studio將moudle變成jar的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
Kotlin中關(guān)于內(nèi)聯(lián)函數(shù)的一些理解分享
這篇文章主要給大家介紹了關(guān)于Kotlin中內(nèi)聯(lián)函數(shù)的一些理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android 中View.onDraw(Canvas canvas)的使用方法
這篇文章主要介紹了Android 中View.onDraw(Canvas canvas)的使用方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android應(yīng)用開(kāi)發(fā)中Fragment的靜態(tài)加載與動(dòng)態(tài)加載實(shí)例
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中Fragment的靜態(tài)加載與動(dòng)態(tài)加載實(shí)例,例子中包括動(dòng)態(tài)的添加更新以及刪除Fragment等操作,很有借鑒意義,需要的朋友可以參考下2016-02-02
詳解Android中用于線程處理的AsyncTask類(lèi)的用法及源碼
這篇文章主要介紹了Android中用于線程處理的AsyncTask類(lèi)的用法及源碼,講到了實(shí)現(xiàn)AsyncTask中所用到的Handler及線程池等要點(diǎn),需要的朋友可以參考下2016-05-05
Android Studio做超好玩的拼圖游戲 附送詳細(xì)注釋源碼
這篇文章主要介紹了用Android Studio做的一個(gè)超好玩的拼圖游戲,你是0基礎(chǔ)Android小白也能包你學(xué)會(huì),另外附送超詳細(xì)注釋的源碼,建議收藏!2021-08-08
Android動(dòng)效Compose貝塞爾曲線動(dòng)畫(huà)規(guī)格詳解
這篇文章主要為大家介紹了Android動(dòng)效Compose貝塞爾曲線動(dòng)畫(huà)規(guī)格詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

