溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

發(fā)布時間:2021-10-18 16:23:56 來源:億速云 閱讀:176 作者:iii 欄目:開發(fā)技術(shù)

本篇內(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é)果

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

下面是具體步驟

一、導(dǎo)入okhttp和okio的依賴

1.打開File-Project Structure-Dependencies,

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

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

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio


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

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

4.okio同上

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

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

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

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

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

二、禁用掉明文流量請求的檢查

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>

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

2.在manifests-AndroidManifest.xml中添加剛才創(chuàng)建的nettools.xml

android:networkSecurityConfig="@xml/nettools"

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

三、添加網(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" />```

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

四、代碼實(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ī)

Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio

到此,相信大家對“Android網(wǎng)絡(luò)請求框架解析之什么是okhttp與okio”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(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)容。

AI