android技术

Rxjava+Okhttp+Retrofit最简单案例

2018-02-22  本文已影响11人  最黑暗的自己

案例抓取豆瓣电影top

public class MainActivity extends AppCompatActivity {
    public static final String BASE_URL = "https://api.douban.com/v2/movie/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        request();
    }

    private void request() {
// 1.创建 OKHttpClient

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        builder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS);//连接超时时间
//        builder.writeTimeout(DEFAULT_TIME_OUT,TimeUnit.SECONDS);//写操作 超时时间
//        builder.readTimeout(DEFAULT_TIME_OUT,TimeUnit.SECONDS);//读操作超时时间

        // 添加公共参数拦截器
        BasicParamsInterceptor basicParamsInterceptor = new BasicParamsInterceptor.Builder()
                .addHeaderParam("userName","")//添加公共参数
                .addHeaderParam("device","")
                .build();

        builder.addInterceptor(basicParamsInterceptor);

//2.Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
//获取接口实例
        MovieService  movieService = retrofit.create(MovieService.class);
//调用方法得到一个Call
        //Call<MovieSubject> call = movieService.getTop250(0,20);

        //进行网络请求
//        call.enqueue(new Callback<MovieSubject>() {
//            public void onResponse(Call<MovieSubject> call, Response<MovieSubject> response) {
//                List<Movie> movies = response.body().subjects;
//                for (Movie movie:movies) {
//                    Log.i("test",movie.title);
//                }
//
//
//            }
//            public void onFailure(Call<MovieSubject> call, Throwable t) {
//                t.printStackTrace();
//            }
//        });

//3.Rxjava
//被观察者
        Subscription subscription = movieService.getTop250(0,20)
                .subscribeOn(Schedulers.io())// 指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())// 指定 Subscriber 的回调发生在主线程
//注册
//Subscriber观察者
                .subscribe(new Subscriber<MovieSubject>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {

                    }
                    @Override
                    public void onNext(MovieSubject movieSubject) {
                        List<Movie> movies = movieSubject.subjects;
                        for (Movie movie:movies) {
                            Log.i("test",movie.title);
                        }
                    }
                });


    }
}
public interface MovieService {
    @GET("top250")
   // Call<MovieSubject> getTop250(@Query("start") int start, @Query("count")int count);
    Observable<MovieSubject> getTop250(@Query("start") int start, @Query("count")int count);
}
上一篇下一篇

猜你喜欢

热点阅读