The fourth unit Rxjava, Rxandroid and the use of Retrofit

What is the observer pattern

During the auction, everyone bids each other, and the auctioneer observes the highest bid price and then informs other bidders to bid. This is an observer model.

For the observer model, there must be an observer and an observer.

Observer mode (Observer), also called publish-subscribe mode (Publish/Subscribe), defines a one-to-many dependency relationship between objects, so that whenever an object changes state, all objects that depend on it will be notified and Auto update

Introduction to Rxjava

https://www.jianshu.com/p/a406b94f3188

Detailed use of Rxjava

https://www.jianshu.com/p/cd984dd5aae8

1, the use of creation operators Rxjava just and fromArray and delay or interval creation

Android RxJava: Detailed explanation of the most basic operators-creating operators

https://www.jianshu.com/p/e19f8ed863b1

2, the use of transformation operators

Function
Process (ie transform) events in the sequence of events/the entire sequence of events to transform them into different events/the entire sequence of events
https://www.jianshu.com/p/904c14d253ba

3. Combination/Merge Operator

https://www.jianshu.com/p/c2a7c03da16d

4. Functional operators

Assist the Observable to fulfill some functional requirements when sending events

Introduction to Retrofit

https://www.jianshu.com/p/a3e162261ab6

Introduction to Retrofit's annotations

https://www.jianshu.com/p/7687365aa946

Interpretation of Retrofit's source code

https://www.jianshu.com/p/0c055ad46b6c

The combined use of Rxjava and Retrofit

package com.fenghongzhang.rxjava_retrofit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    
    

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit.Builder builder = new Retrofit.Builder();
        builder.addConverterFactory(GsonConverterFactory.create());
        builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        builder.baseUrl("http://www.qubaobei.com/");
        Retrofit retrofit = builder.build();

        FoodInterface foodInterface = retrofit.create(FoodInterface.class);

        Observable<Food> food = foodInterface.getFood();

        food.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Food>() {
    
    
                @Override
                public void onSubscribe(Disposable d) {
    
    
                    Log.i(TAG, "onSubscribe: ");
                }

                @Override
                public void onNext(Food food) {
    
    
                    Log.i(TAG, "onNext: "+food.toString());
                    Toast.makeText(MainActivity.this, food.toString(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(Throwable e) {
    
    

                }

                @Override
                public void onComplete() {
    
    

                }
            });


    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/shuai_ge_feng/article/details/113754324