preface
-
Rxjava
Because of itsChain call based on event flow, simple logic & easy to useThe characteristics of the deepAndroid
Developer welcome.
If you are not familiar with RxJava, check out Android: a clear and easy-to-understand introduction to RxJava
-
RxJava
That’s why it’s so popularProvides rich & powerful operators that can fulfill almost all functional requirements - Today, I’m going to bring you
Rxjava
Of the create operatorCommon development application scenarios: Polling requirements, and combined withRetrofit
与RxJava
Implementation, I hope you like it.
- This series of articles is based on
Rxjava 2.0
- In the coming days, I will continue to publish a series of articles on Rxjava 2.0 in Android, including principles, operators, application scenarios, back pressure, etc. If you are interested, please continue to follow Carson_Ho’s Android development notes!!
directory
1. Requirement scenario
The delay creation operators interval () and intervalRange () are available in Android Rxjava
2. Function Description
This paper will use Get method to kingsoft POWERword API according to the specified time to repeatedly send network requests, so as to simulate the implementation of polling requirements
Gson is used for data parsing
3. Implementation
Next I’ll implement the polling requirements in combination with Retrofit and RxJava
3.1 Procedure description
- Add the dependent
- Create a class that receives data from the server
- Create an interface for describing network requests (as opposed to tradition
Retrofit
Form) - create
Retrofit
The instance - Create a network request interface instance and configure the network request parameters (different from tradition)
Retrofit
Form) - Sending network requests (different from tradition
Retrofit
Form) - Sending network Requests
- Process the returned data
This example focuses on the polling requirements of RxJava. For Retrofit, see article: This is a very detailed tutorial on using Retrofit 2.0.
3.2 Procedure Implementation
Step 1: Add dependencies
A. Add Retrofit library dependencies in Gradle
build.gradle
Dependencies {/ / Android support Rxjava / / here it is important to note that the use of RxJava2 version the compile 'IO. Reactivex. RxJava2: Rxjava: 2.0.1' compile 'IO. Reactivex. Rxjava2: rxandroid: 2.0.1' / / Android support to Retrofit the compile 'com. Squareup. Retrofit2: Retrofit: 2.1.0' / / cohesion Retrofit & RxJava / / here it is important to note that the use of RxJava2 version the compile 'com. Jakewharton. Retrofit: retrofit2 RxJava2 - adapter: 1.0.0' / / support Gson parsing The compile 'com. Squareup. Retrofit2: converter - gson: 2.1.0'}Copy the code
B. Add the network permission androidmanifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Copy the code
Step 2: Create the class that receives the data returned by the server
- powerword
API
The data format is described as follows:
/ / http://fy.iciba.com/ajax.php / / URL examples http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=hello%20world URL template / / parameter description: // a: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // f: fy // Text type: Ja for Japanese, zh for Chinese, EN for English, KO for Korean, DE for German, ES for Spanish, FR for French, auto // w for automaticCopy the code
- The sample
- According to kingsoft POWERword API data format, create a class to receive the data returned by the server:
Translation.java
public class Translation { private int status; private content content; private static class content { private String from; private String to; private String vendor; private String out; private int errNo; } public void show() {log.d ("RxJava", content.out); }}Copy the code
Step 3: Create an interface to describe network requests
Use the annotation + Observable<… > Interface Description Network request parameters
GetRequest_Interface.java
public interface GetRequest_Interface { @GET("ajax.php? a=fy&f=auto&t=auto&w=hi%20world") Observable<Translation> getCall(); // Retrofit breaks the URL of the web request into two parts: // If the url in the interface is a complete URL, the url in the Retrofit object can be ignored. // Observable<... > interface // getCall() is the method to accept network request data}Copy the code
The next steps are inRxJavafixRxjava.javaInternal implementation (see comments)
RxJavafixRxjava.java
public class RxJavafixRxjava extends AppCompatActivity { private static final String TAG = "Rxjava"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * Step 1: Delay sending * by interval () note: **/ observable. interval(2,1, timeunit.seconds) // // Parameter 1 = first delay time; // Parameter 2 = interval number; // parameter 3 = time; /* * Step 2: /* * step 2: /* * step 2: Send a network request every time a number is sent (doOnNext () is called before the Next event is executed) * that is, send a network request every second before a number is produced, **/.doonNext (new Consumer<Long>() {@override public void Accept (Long integer) throws Exception {log.d (TAG, "First" + integer + "second poll"); /* * Step 3: Send network request via Retrofit **/ / a. Retrofit = new Retrofit.builder ().baseurl ("http://fy.iciba.com/") // Set the network request Url AddConverterFactory (GsonConverterFactory. The create ()) / / set using Gson parsing (remember to join rely on) AddCallAdapterFactory (RxJava2CallAdapterFactory. The create ()) / / support RxJava. The build (); GetRequest_Interface Request = retrofit.create(getrequest_interface.class); // Observable<... Observable<Translation> Observable = request.getCall(); / / d. request sent via thread switching network observables. SubscribeOn Schedulers. IO () () / / requests for network switch to the IO thread observeOn (AndroidSchedulers. MainThread ()) Subscribe(new Observer<Translation>() {@override public void onSubscribe(Disposable d) {} @override public void onNext(Translation result) { // e. Result.show (); } @override public void onError(Throwable e) {log. d(TAG, "request failed "); } @Override public void onComplete() { } }); } }).subscribe(new Observer<Long>() { @Override public void onSubscribe(Disposable d) { } @Override public void OnNext (Long value) {} @override public void onError(Throwable e) {log.d (TAG, "response to Error "); } @override public void onComplete() {log.d (TAG, "response to Complete event "); }}); }}Copy the code
3.3 Test Results
4. The Demo address
Carson_Ho Github address = RxJava2
5. To summarize
- This article mainly explains
Rxjava
Create an actual development requirements scenario for the operator: poll the requirements, and combineRetrofit
与RxJava
implementation - In the coming days, I will continue to publish a series of articles on Rxjava 2.0 in Android, including principles, operators, application scenarios, back pressure, etc. If you are interested, please continue to follow Carson_Ho’s Android development notes!!
Thumb up, please! Because your encouragement is the biggest power that I write!
Android Development: The most comprehensive and easy-to-understand Android screen adaptation solution For Android Event distribution Android development: the most comprehensive, the most easy-to-understand Webview Android development: JSON introduction and the most comprehensive parsing method! BroadcastReceiver Is the most comprehensive version of Android’s BroadcastReceiver
Welcome to attentionCarson_HoJane books!
Share the dry things about Android development from time to time, the pursuit of short, flat, fast, but there is no lack of depth.