30 seconds to start the new generation of Http request artifact RxHttp, IDEA is too strong

Posted by mbbout on Thu, 16 Dec 2021 07:10:31 +0100

     sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}

dependencies {
implementation 'com.github.liujingxing.rxhttp:rxhttp:2.6.0'
implementation ‘com.squareup.okhttp3:okhttp:4.9.0’ //rxhttp v2. Since version 2.2, you need to manually rely on okhttp
kapt ‘com.github.liujingxing.rxhttp:rxhttp-compiler:2.6.0 '/ / generate RxHttp class, pure Java project, please use annotationProcessor instead of kapt
}

**2,Optional**

android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
rxhttp_package: 'rxhttp', / / not required. Specify the package name of rxhttp class
//Pass in the rxjava version you depend on. You can pass in rxjava2 and rxjava3. When relying on rxjava, you must
rxhttp_rxjava: 'rxjava3'

            ]
        }
    }
}

}
dependencies {
implementation ‘com.github.liujingxing.rxlife:rxlife-coroutine:2.1.0 '/ / manage the collaboration life cycle, destroy the page, and close the request

//Rxjava2 (one of rxjava2 / rxjava3, required when asXxx method is used)
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.github.liujingxing.rxlife:rxlife-rxjava2:2.1.0' //Manage RxJava2 life cycle, page destruction, and close requests

//rxjava3
implementation 'io.reactivex.rxjava3:rxjava:3.0.6'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'com.github.liujingxing.rxlife:rxlife-rxjava3:2.1.0' //Manage RxJava3 life cycle, page destruction, and close requests

//It's not necessary. You can select RxHttp according to your needs. By default, GsonConverter is built in
implementation 'com.github.liujingxing.rxhttp:converter-fastjson:2.6.0'
implementation 'com.github.liujingxing.rxhttp:converter-jackson:2.6.0'
implementation 'com.github.liujingxing.rxhttp:converter-moshi:2.6.0'
implementation 'com.github.liujingxing.rxhttp:converter-protobuf:2.6.0'
implementation 'com.github.liujingxing.rxhttp:converter-simplexml:2.6.0'

}

last, rebuild once(This step is required) ,It will be generated automatically RxHttp class

Trilogy interpretation
=====

I believe many people have questions here

*   If I want to send Post And other ways to request it?
*   What about file upload, download and progress monitoring?
*   Do I want to get a custom data type?

How can this be achieved through a trilogy? Don't worry. Let's explain it one by one

The first step is to determine the request method
----------

In the above example, we call`RxHttp.get("http://... ") 'statement, in which the' Get 'operator represents the Get request. Therefore, we can guess that to send a post request, we only need to call the' post 'operator. However, we only guessed half right. Why do we say so? Post requests are commonly divided into two types, one is in the form of post and the other is in the form of Json string. For this reason, [RxHttp] () Two operators for sending post requests are provided, namely 'postForm' and 'postJosn'. In this case, we can send post requests in this way

RxHttp.postForm("http: / /...") / / send a Post request in form form
. asString() / / return String type
. Subscribe (s) - > {/ / subscribe to observers,
//Request succeeded
}, throwable -> {
//Request failed
});

RxHttp.postJson("http: / /...") / / send a Post request in the form of a Json string
. asString() / / return String type
. Subscribe (s) - > {/ / subscribe to observers,
//Request succeeded
}, throwable -> {
//Request failed
});

If you want to send Delete,Put And other requests, similarly, as follows:

RxHttp.deleteForm("http://...")
RxHttp.deleteJson("http://...")
RxHttp.putForm("http://...")
RxHttp.putJson("http://...")
//Other request methods are the same as above

Finally, let's take a look,[RxHttp]( )Which request methods are provided, as follows: ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/30684376f1a04c02b5694d647ee8f5d8~tplv-k3u1fbpfcp-zoom-1.image) among them, 'get', 'postForm' and 'postjason' have been mentioned above, and others are the same, which will not be described here.

The request method is determined. How to add parameters or avatar information? so easy!!!,Just call`add`,`addHeader`OK, as follows:

RxHttp.postForm("http: / /...") / / send a Post request in form form
. add("key", "value") / / add request parameters. This method can be called multiple times
. addHeader("headerKey", "headerValue") / / add a request header parameter. This method can be called multiple times
. asString() / / return String type
. Subscribe (s) - > {/ / subscribe to observers,
//Request succeeded
}, throwable -> {
//Request failed
});

The second step is to determine the return data type
------------

above`asString`Operators represent returns String String type,[RxHttp]( )Provides a range of`asXXX`Operator, as follows:

![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/231eec1aad3b40d3a48c560051af72e7~tplv-k3u1fbpfcp-zoom-1.image) among them, asBoolean, asInteger, asLong, asString, etc. are easy to understand, which is to return the packing type of the basic type,. This is not too much to explain. Here, let's focus on the three operators' asClass', 'asList' and 'asDownload'.

### asClass

In display development, we return more user-defined data types. For example, we want to get one Student Object, at this time, we can use`asClass`Operator, as follows:

RxHttp.get("http: / /...") / / send a get request
. asClass(Student.class) / / specify the User type data to be returned
. Subscribe (student - > {/ / subscribe to the observer,
//The request is successful. Here, student is the student object
}, throwable -> {
//Request failed
});

### asList

However, if we want to get a series Student What about the object? use asObject Obviously it doesn't work. It's going to be used at this time`asList`The operator is as follows:

RxHttp.get("http: / /...") / / send a get request
. asList(Student.class) / / specify the User type data to be returned
. Subscribe (students - > {/ / subscribe to observers,
//The request is successful, where students is the List object
}, throwable -> {
//Request failed
});

> Note: asXXX Operator, which specifies the request by default`Schedulers.io()`Thread execution

### asDownload

When we need to download files, we use this operator, as follows:

RxHttp.get("http: / /...") / / get request

Finally, I want to say

Why can't many programmers be architects?
1. Good and healthy career planning is important, but most people ignore it
2. The habit of learning is very important. Perseverance is the right solution.
3. Programming thinking failed to improve to a higher level, limited to coding and business, and did not consider selection and expansion
4. There is no good architect to guide and cultivate. The circle has a great impact on the growth of programmers.

Golden nine silver ten interview season, job hopping season, sorting out interview questions has become my habit for many years! Here, some friends and I have specially sorted out a systematic and comprehensive learning material for quickly upgrading to Android senior engineer. It covers the basic learning skills of Android - some necessary learning skills for advanced Android architects.

Attached: We collected 20 sets of real Android interview questions for first and second tier Internet companies (including BAT, Xiaomi, Huawei, meituan and didi) and I sorted out my android review notes (including Android basic knowledge points, Android extended knowledge points, Android source code analysis, design pattern summary, Gradle knowledge points and common algorithm questions.)

It contains self-study programming routes in different directions, interview questions collection / face experience, and a series of technical articles. The resources are constantly updated

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

353)]

It contains self-study programming routes in different directions, interview questions collection / face experience, and a series of technical articles. The resources are constantly updated

CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code

Topics: Android Design Pattern http IDEA