rxjava+retrofit简单入门demo
1、rxjava的三个行为
1、观察者
Subscriber
2、被观察者
Observable
3、订阅关系
observable.subscribe(subscriber)
2、rxjava示例
请求api
http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location=北京&output=json&ak=A72e372de05e63c8740b2622d0ed8ab1
代码如下:
1、ApiService代码
public interface ApiService {
/**
* 请求示例:
* http://api.map.baidu.com/telematics/v3/movie?qt=hot_movie&location=北京&output=json&ak=A72e372de05e63c8740b2622d0ed8ab1
* @param query
* @param location
* @param output
* @param ak
* @return
*/
@GET("/telematics/v3/movie/{qt}/{location}/{output}/{ak}")
Observable<Movie> getMovies(@Query("qt") String query,
@Query("location") String location,
@Query("output") String output,
@Query("ak") String ak);
}
2、NetWork请求代码
public class NetWrok {
private static OkHttpClient okHttpClient = new OkHttpClient();
private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
private static retrofit2.CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();
public static ApiService apiService;
public static ApiService getMovieApi() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl("http://api.map.baidu.com/")
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJavaCallAdapterFactory)
.build();
apiService = retrofit.create(ApiService.class);
}
return apiService;
}
}
3、调用代码
package com.example.administrator.myrxjava;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import adapter.FunAdapter;
import bean.Movie;
import network.NetWrok;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerview;
private SwipeRefreshLayout swiperefresh;
private Button button;
private FunAdapter adapter;
private ArrayList<Movie.Movies> list;
protected Subscription subscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview= (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview.setLayoutManager(linearLayoutManager);
swiperefresh= (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
button= (Button) findViewById(R.id.button);
list=new ArrayList<Movie.Movies>();
adapter=new FunAdapter(MainActivity.this,list);
recyclerview.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
swiperefresh.setRefreshing(true);
initDate();
}
});
}
private void initDate() {
subscription = NetWrok.getMovieApi()
.getMovies("hot_movie","北京","json","A72e372de05e63c8740b2622d0ed8ab1")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
Observer<Movie> observer = new Observer<Movie>() {
@Override
public void onCompleted() {
swiperefresh.setRefreshing(false);
Log.i("--rxjava--","onCompleted");
}
@Override
public void onError(Throwable e) {
Log.i("--rxjava--e",e.toString());
}
@Override
public void onNext(Movie images) {
Log.i("--rxjava--onNext",images.getResult().getMovie().size()+"");
adapter.setImages(images.getResult().getMovie());
}
};
}
代码下载地址
http://download.csdn.net/detail/androidxiaogang/9741484