Android网络请求框架解析之什么是okhttp与okio

发布时间:2021-10-18 16:23:56 作者:iii
来源:亿速云 阅读:162

本篇内容主要讲解“Android网络请求框架解析之什么是okhttp与okio”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android网络请求框架解析之什么是okhttp与okio”吧!

安卓网络请求

先看一下今天的大纲

Android网络请求框架解析之什么是okhttp与okio

下面是具体步骤

一、导入okhttp和okio的依赖

1.打开File-Project Structure-Dependencies,

Android网络请求框架解析之什么是okhttp与okio

2.选择自己的程序文件,点击加号,选择Library Dependency

Android网络请求框架解析之什么是okhttp与okio


3.搜索okhttp,选择Com.squareup.okhttp3,点击ok按钮,此时可能需要较长时间

Android网络请求框架解析之什么是okhttp与okio

4.okio同上

Android网络请求框架解析之什么是okhttp与okio

5.应用,确认

Android网络请求框架解析之什么是okhttp与okio

6.此时我们可以看到Gradle Scripts-build.gradle (Module: My_Application.app)多了两个依赖
Module: My_Application.app是自己对应的app

Android网络请求框架解析之什么是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网络请求框架解析之什么是okhttp与okio

2.在manifests-AndroidManifest.xml中添加刚才创建的nettools.xml

android:networkSecurityConfig="@xml/nettools"

Android网络请求框架解析之什么是okhttp与okio

三、添加网络请求权限

在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网络请求框架解析之什么是okhttp与okio

四、代码实现

1.主代码的实现

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.主布局的实现

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>

五、运行结果

如果运行失败可能是模拟器的问题,建议换模拟器或直接用真机

Android网络请求框架解析之什么是okhttp与okio

到此,相信大家对“Android网络请求框架解析之什么是okhttp与okio”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. OkHttp解析
  2. Android开发之OkHttp3.4.x

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android okhttp okio

上一篇:怎么实现Spring Core动态代理

下一篇:怎么实现Nginx+Tomcat负载均衡集群

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》