您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio”吧!
安卓網(wǎng)絡(luò)請求
先看一下今天的大綱
導(dǎo)入okhttp和okio依賴
禁用掉明文流量請求的檢查
添加訪問權(quán)限
布局及代碼實(shí)現(xiàn)
運(yùn)行結(jié)果
下面是具體步驟
1.打開File-Project Structure-Dependencies,
2.選擇自己的程序文件,點(diǎn)擊加號,選擇Library Dependency
3.搜索okhttp,選擇Com.squareup.okhttp3,點(diǎn)擊ok按鈕,此時可能需要較長時間
4.okio同上
5.應(yīng)用,確認(rèn)
6.此時我們可以看到Gradle Scripts-build.gradle (Module: My_Application.app)多了兩個依賴
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"
在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" />```
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(); } }
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)行失敗可能是模擬器的問題,建議換模擬器或直接用真機(jī)
到此,相信大家對“Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(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)容。