欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年10月18日 14:12:46   作者:自動(dòng)2004郝金輝  
HTTP是現(xiàn)代應(yīng)用常用的一種交換數(shù)據(jù)和媒體的網(wǎng)絡(luò)方式,高效地使用HTTP能讓資源加載更快,節(jié)省帶寬,OkHttp是一個(gè)高效的HTTP客戶端,下面這篇文章主要給大家介紹了關(guān)于OkHttp如何用于安卓網(wǎng)絡(luò)請(qǐng)求,需要的朋友可以參考下

安卓網(wǎng)絡(luò)請(qǐng)求

先看一下今天的大綱

  • 導(dǎo)入okhttp和okio依賴
  • 禁用掉明文流量請(qǐng)求的檢查
  • 添加訪問權(quán)限
  • 布局及代碼實(shí)現(xiàn)
  • 運(yùn)行結(jié)果

在這里插入圖片描述

下面是具體步驟

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

1.打開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è)依賴
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)行失敗可能是模擬器的問題,建議換模擬器或直接用真機(jī)

在這里插入圖片描述

到此這篇關(guān)于Android 網(wǎng)絡(luò)請(qǐng)求框架解析之okhttp與okio的文章就介紹到這了,更多相關(guān)Android okhttp 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論