Demo data source is a free API for aggregated data, address: https://www.juhe.cn/
Cooperate with Retrofit to complete data request
The example is relatively simple, and there is nothing complicated to use.
It is a simple network data acquisition.
For some commonly used operators, you can refer to the official documentation:
ReactiveX/RxJava Documentation Chinese Version
Regarding the introduction of RxJava, I am also a novice, and I dare not speak falsely. I will share with you the great god blog circulating on the Internet:
The throw line is big:
RxJava Explained for Android Developers
Explain RxJava in simple terms (1: Basics)
In-depth explanation of RxJava (two: operators)
Explain RxJava in a simple way (three -- the benefits of responsiveness)
In-depth RxJava (four - using reactive programming in Android)
First, introduce RxJava and RxAndroid dependencies into the project:
compile 'io.reactivex:rxjava:1.0.14' compile 'io.reactivex:rxandroid:1.1.0'
The life cycle:
compile 'com.trello:rxlifecycle:0.4.0' compile 'com.trello:rxlifecycle-components:0.4.0'
Introduce Retrofit dependencies
compile 'com.squareup.retrofit2:retrofit:2.0.0' compile 'com.squareup.retrofit2:converter-gson:2.0.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
Then you can write code.
Take a look at the running screenshot:
Api can go to the official website of aggregated data to apply.
This is the basic display of ListView, so the program steps are very simple:
First, according to the json data, write the entity class. (Quickly generated with Gson plugin)
2. Create a layout based on the data to be displayed.
Third, write Adapter.
Fourth, then request and return data from the network.
5. Create an Adapter based on the data and bind it to the listview for display.
These are all GET requests, so the writing is the same:
Create an interface:
public interface WeatherApi { @GET("/onebox/weather/query?") Observable<Weather> getWeatherInfo(@Query("cityname") String phone, @Query("key") String key); }
Create Retrofit:
public static WeatherApi getWeatherApi() { if (weatherApi == null) { Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://op.juhe.cn") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); weatherApi = retrofit.create(WeatherApi.class); } return weatherApi; }
Subscribe to trigger code in Activity:
RxView.clicks(btn_check).throttleFirst(3, TimeUnit.SECONDS) .subscribe(new Action1<Void>() { @Override public void call(Void aVoid) { NetWork.getWeatherApi() .getWeatherInfo(et_city_name.getText().toString(), API_KEY) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Weather>() { @Override public void call(Weather weather) { setDispaly(weather); } }); } });
The weather API is in the code and can be used directly. Since it is a free interface, everyone can apply, but the aggregated data needs to verify the ID card.
Examples can be downloaded for reference on git.
https://github.com/VongVia1209/RxAndroid_Demo_With_jvhe
Article source: reprint http://blog.csdn.net/castledrv/article/details/51333736