Retrofit+Rxjava Complete Network Request

A recent project is using this networking, it is very convenient to use, and code management is very convenient. I used studio to add it to grade first
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.7'
The code is as follows
public class RetrofitHelper {

private static final String BASE_URL = "Host website";

//okhttp client object
private static OkHttpClient mOkHttpClient;

static {
    //This is the first execution
    initOkhttp();
}

//Initialize okhttp client, synchronized to ensure singleton
private static void initOkhttp() {
    if (mOkHttpClient == null) {
        synchronized (RetrofitHelper.class) {
            if (mOkHttpClient == null) {
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
                mOkHttpClient = builder
                        .retryOnConnectionFailure(true)     //Automatic reconnection when connection fails
                        .connectTimeout(10, TimeUnit.SECONDS)   //Set the timeout time of connecting to the network, in seconds
                        .readTimeout(20, TimeUnit.SECONDS)  //Read data timeout
                        .writeTimeout(20, TimeUnit.SECONDS)//Write timeout
                        .build();           //Caching and interceptors need not be added if there is no requirement
            }
        }
    }
}

public static MaYiApiService getApiService() {
    Retrofit retrofit = new Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(mOkHttpClient)  //Transfer okhttp client
            .baseUrl(BASE_URL)  //Transmit ip address
            .build();
    return retrofit.create(MaYiApiService.class);
}

After encapsulating the interface class, you can use it. Maybe some networking requests need to add headers. If you use interceptors, each network has a header, which is similar to mine
The requirements do not match. I added them manually. If you need to add headers, just write an interceptor

The following is the interface class I wrote:
public interface WXApiService {

@GET("cgi-bin/token")
Flowable<WxTokenBean> getWxtoken(@Query("grant_type") String grant_type,
                                 @Query("appid") String appid,
                                 @Query("secret") String secret);

}

Use in project
protected CompositeDisposable mDisposable = new CompositeDisposable();
protected WXApiService mWXApiService = RetrofitHelper.getApi();

                        mDisposable.add(mWXApiService.getWxMessage(mToken,body).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<ResponseBody>(){
                            @Override
                            public void accept(@NonNull ResponseBody responseBody) throws Exception {
                         Log.i("msg","success");
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(@NonNull Throwable throwable) throws Exception {
                            Log.i("msg","fail");
                            }
                        }));

Tags: Retrofit OkHttp network

Posted on Fri, 17 Jul 2020 12:11:06 -0400 by kaos057