• Android学习笔记 42. RxJava基本使用


    Android学习笔记

    Android基础开发——RxJava学习

    42. RxJava基本使用

    42.1 RX思维

    为什么学习RxJava? → 改变思维(响应式编程思维),编程效率提升。

    经典架构官方地址:https://github.com/ReactiveX/RxJava

    在这里插入图片描述

    Rx系列官网:https://reactivex.io/

    在这里插入图片描述

    RX → 反应式

    42.2 什么是响应式编程?

    RX → 反应式

    reactive == 反应式 == 响应式编程思维

    RX == 响应式编程思维

    RX思维的学习 → 【一劳永逸】

    【什么是响应式编程?】

    (点击登录按钮)起点 和 终点(登录成功、失败)

    细化: 起点 和 终点

    (起点) 点击按钮登录 --------> 请求服务器 --------> 服务器响应 ----------> 解析响应的JSON数据(终点)【登录成功 或 登录失败】

    → →

    问:什么是响应式编程?

    答:根据上一层的响应 来 影响下一层的变化。

    42.3 使用Rx思维RxJava下载图片

    创建新项目

    在这里插入图片描述

    添加依赖

    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    
        implementation "io.reactivex.rxjava3:rxjava:3.1.5"
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    同步

    布局

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/image"
            />
    
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="图片显示加载功能"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:onClick="showImageAction"
            />
    
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    MainActivity.java

    package com.dingjiaxiong.myrxjava;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.ImageView;
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
    import io.reactivex.rxjava3.annotations.NonNull;
    import io.reactivex.rxjava3.core.Observable;
    import io.reactivex.rxjava3.core.Observer;
    import io.reactivex.rxjava3.disposables.Disposable;
    import io.reactivex.rxjava3.functions.Consumer;
    import io.reactivex.rxjava3.functions.Function;
    import io.reactivex.rxjava3.schedulers.Schedulers;
    
    public class MainActivity extends AppCompatActivity {
    
        // 打印logcat日志的标签
        private final String TAG = MainActivity.class.getSimpleName();
    
        // 网络图片的链接地址
        private final static String PATH = "http://pcsource.upupoo.com/theme/2000858833/previewFix.jpg";
    
        // 弹出加载框(正在加载中...)
        private ProgressDialog progressDialog;
    
        // ImageView控件,用来显示结果图像
        private ImageView image;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            image = findViewById(R.id.image);
        }
    
        /**
         * 图片显示加载功能
         *
         * @param view
         */
        public void showImageAction(View view) {
    
             //如果采用传统开发方式,后面的开发者接手前面开发者的代码,就很痛苦(弊端)
            /**
             * TODO RX思维
             *
             * 起点 和 终点
             *
             * RxJava RXJS RxXXX  RX系列框架 为什么把所有函数都成为操作符 因为我们的函数要去操作  从起点 流向 终点7
             *
             */
    
    
            // 起点
            Observable.just(PATH)
    
    
                    // 需求:001 图片下载需求  PATH ---》 Bitmap
                    .map(new Function<String, Bitmap>() {
                        @NonNull
                        @Override
                        public Bitmap apply(@NonNull String path) throws Exception {
                            try {
                                Thread.sleep(2000); // 睡眠2秒钟
    
                                URL url = new URL(path);
                                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                                httpURLConnection.setConnectTimeout(5000); // 设置请求连接时长 5秒
                                int responseCode = httpURLConnection.getResponseCode(); // 才开始 request    拿到服务器的响应  200成功  404有问题 ...
                                if (responseCode == HttpURLConnection.HTTP_OK) {
                                    InputStream inputStream = httpURLConnection.getInputStream();
                                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                    return bitmap;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return null;
                        }
                    })
    
                    // 给上面的分配异步线程(图片下载操作)
                    .subscribeOn(Schedulers.io())
    
    
                    // 终点分配 Android主线程
                    .observeOn(AndroidSchedulers.mainThread())
    
                    // 关联:观察者设计模式   关联 起点 和 终点  ==  订阅
                    .subscribe(
    
                            // 终点
                            new Observer<Bitmap>() {
    
                                // 订阅成功
                                @Override
                                public void onSubscribe(Disposable d) {
    
                                }
    
                                // 上一层给我的响应
                                @Override
                                public void onNext(Bitmap bitmap) {
                                    image.setImageBitmap(bitmap); // 显示到控件上
                                }
    
                                // 链条思维发生了异常
                                @Override
                                public void onError(Throwable e) {
    
                                }
    
                                // 整个链条全部结束
                                @Override
                                public void onComplete() {
                                }
                            });
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    配置网络权限

    在这里插入图片描述

    运行

    报错了

    在这里插入图片描述

    开始谷歌

    在这里插入图片描述

    原来如此。

    配置一下

    1.在res文件夹下创建一个xml文件夹,然后创建一个network_security_config.xml文件,文件内容如下:

    
    
        
    
    
    • 1
    • 2
    • 3
    • 4

    2.接着,在AndroidManifest.xml文件下的application标签增加以下属性:

    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.在AndroidManifest.xml配置文件的标签中直接插入

        android:usesCleartextTraffic="true"
    
    • 1

    在这里插入图片描述

    在这里插入图片描述

    再次运行

    在这里插入图片描述

    增加加载框,显示【正在加载中】

    在这里插入图片描述

    在整个链条思维结束后,隐藏加载框

    在这里插入图片描述

    再次运行

    在这里插入图片描述

    奈斯。

  • 相关阅读:
    postman的使用
    基于I/Q数据的5G控制信道盲检
    Latex内容超出页面 行 的长度
    shiro认证机制及认证原理
    Eigen入门
    UE4 C++ 笔记(二):基础知识
    webpack初体验
    高效能团队的Java研发规范(进阶版)
    css3新单位vw、vh、vmin、vmax的使用介绍
    GitHub
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126296806