码迷,mamicode.com
首页 > 移动开发 > 详细

Android--Retrofit+RxJava

时间:2016-11-03 13:24:24      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:art   idg   void   ttext   entity   scheduler   view   tle   click   

1,现在响应式编程也是越来越多在项目中使用了,刚好上篇我们简单了介绍了一下Retrofit,那么我们来开始试着两个一起用吧,不过不了解RxJava的同学可以先去看一看这个文章(挺好的):http://gank.io/post/560e15be2dca930e00da1083#toc_27

2,接着我们上一个项目的写,怎么添加RxJava到我们网络请求框架中呢,首先我们得修改Service的代码为

public interface MovieService {
    @GET("top250")
    Observable<MovieEntity> getTopMovie(@Query("start") int start, @Query("count") int count);
}

 然后我们Activity中的getMovie()方法修改为 

package com.qianmo.retrofitdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.qianmo.retrofitdemo.entry.MovieEntity;
import com.qianmo.retrofitdemo.http.MovieService;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.tv_show)
    TextView tvShow;
    @Bind(R.id.btn_request)
    Button btnRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_request})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_request:
                getMovie();
                break;
        }
    }

    //请求网络数据
    private void getMovie() {

//        //https://api.douban.com/v2/movie/top250?start=0&count=10 目标地址
//        String baseUrl = "https://api.douban.com/v2/movie/";
//
//        //创建Retrofit对象
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(baseUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//
//        MovieService movieService = retrofit.create(MovieService.class);
//        Call<MovieEntity> call = movieService.getTopMovie(0, 10);
//        call.enqueue(new Callback<MovieEntity>() {
//            @Override
//            public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {
//                tvShow.setText(response.body().getTitle());
//            }
//
//            @Override
//            public void onFailure(Call<MovieEntity> call, Throwable t) {
//                tvShow.setText(t.getMessage());
//            }
//        });

        String baseUrl = "https://api.douban.com/v2/movie/";

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        MovieService movieService = retrofit.create(MovieService.class);

        movieService.getTopMovie(0, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MovieEntity>() {
                    @Override
                    public void onCompleted() {
                        Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        tvShow.setText(e.getMessage());
                    }

                    @Override
                    public void onNext(MovieEntity movieEntity) {
                        tvShow.setText(movieEntity.getTitle());
                    }
                });
    }


}

这样我们就简单的把RxJava集成过来了  

Android--Retrofit+RxJava

标签:art   idg   void   ttext   entity   scheduler   view   tle   click   

原文地址:http://www.cnblogs.com/wjtaigwh/p/6025978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!