RxJava和Retrofit结合使用

前言:

前段时间花了点时间学了一下RxJava和Retrofit2,后面在实践中被没有用到,今天一看,呃,都基本上忘掉了。

在这里总结记录一下。

这里调用豆瓣的电影接口拉取一些数据

URL:https://api.douban.com/v2/movie/

 

一、只用Retrofit2发起网络请求

1.给对应的URL定义一个Java接口

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public interface MovieService {  
  2.   
  3.     @GET("top250")//start,count 是接口的参数  
  4.     Call<MovieEntity> getTopMovie(@Query("start") int start, @Query("count") int count);  
  5.   
  6. }  

2.用Retrofit 实例调用接口

 

创建实例

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. Retrofit retrofit = new Retrofit.Builder()  
  2.         .baseUrl(url)  
  3.         .addConverterFactory(GsonConverterFactory.create())  
  4.         .build();  
  5. MovieService movieService = retrofit.create(MovieService.class);  

 

发起请求

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. Call<MovieEntity> call = movieService.getTopMovie(0, 10);  
  2. call.enqueue(new Callback<MovieEntity>() {  
  3.       @Override  
  4.       public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {  
  5.           Toast.makeText(getBaseContext(), "success=" + response.body().toString(), Toast.LENGTH_SHORT).show();  
  6.       }  
  7.   
  8.       @Override  
  9.       public void onFailure(Call<MovieEntity> call, Throwable t) {  
  10.           Toast.makeText(getBaseContext(), "error=" + t.getMessage(), Toast.LENGTH_SHORT).show();  
  11.       }  
  12. });  

用到的实体类

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class MovieEntity implements Serializable {  
  2.   
  3.     private Long mId;  
  4.   
  5.     private String mTitle;  
  6.   
  7.     private String mSummary;  
  8.   
  9.     public Long getmId() {  
  10.         return mId;  
  11.     }  
  12.   
  13.     public void setmId(Long mId) {  
  14.         this.mId = mId;  
  15.     }  
  16.   
  17.     public String getmTitle() {  
  18.         return mTitle;  
  19.     }  
  20.   
  21.     public void setmTitle(String mTitle) {  
  22.         this.mTitle = mTitle;  
  23.     }  
  24.   
  25.     public String getmSummary() {  
  26.         return mSummary;  
  27.     }  
  28.   
  29.     public void setmSummary(String mSummary) {  
  30.         this.mSummary = mSummary;  
  31.     }  
  32. }  


二、用RxJava与Retrofit2配合使用

 

1.把接口对应的Java接口修改如下

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public interface MovieService {  
  2.   
  3. //    @GET("top250")  
  4. //    Call<MovieEntity> getTopMovie(@Query("start") int start, @Query("count") int count);  
  5.   
  6.     @GET("top250")  
  7.     Observable<HttpResult<List<Subject>>> getTopMovie(@Query("start") int start, @Query("count") int count);  
  8.   
  9. }  

2.发起网络请求修改如下,不用Retrofit2的Call回调了,改用RxJava的异步通知机制

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. Retrofit retrofit = new Retrofit.Builder()  
  2.                 .baseUrl(url)  
  3.                 .addConverterFactory(GsonConverterFactory.create())  
  4.                 .addCallAdapterFactory(RxJavaCallAdapterFactory.create())  
  5.                 .build();  
  6. MovieService movieService = retrofit.create(MovieService.class);  
  7. //Call<MovieEntity> call = movieService.getTopMovie(0, 10);  
  8. //call.enqueue(new Callback<MovieEntity>() {  
  9. //@Override  
  10. //public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {  
  11. //    Toast.makeText(getBaseContext(), "success=" + response.body().toString(), Toast.LENGTH_SHORT).show();  
  12. //}  
  13. //  
  14. //@Override  
  15. //public void onFailure(Call<MovieEntity> call, Throwable t) {  
  16. //    Toast.makeText(getBaseContext(), "error=" + t.getMessage(), Toast.LENGTH_SHORT).show();  
  17. //}  
  18. //});  
  19. movieService.getTopMovie(0, 1)  
  20.       .subscribeOn(Schedulers.io())  
  21.       .observeOn(AndroidSchedulers.mainThread())  
  22.       .subscribe(new Subscriber<HttpResult<List<Subject>>>() {  
  23.           @Override  
  24.           public void onCompleted() {  
  25.           }  
  26.   
  27.           @Override  
  28.           public void onError(Throwable e) {  
  29.   
  30.           }  
  31.   
  32.           @Override  
  33.           public void onNext(HttpResult<List<Subject>> listHttpResult) {  
  34.               Toast.makeText(getBaseContext(), "result=" + listHttpResult.getTitle(), Toast.LENGTH_SHORT).show();  
  35.           }  
  36.       });  

3.用到的HttpResult,Subject。这里用这两个类代替了之前的MovieEntity

 

HttpResult.java

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class HttpResult<T> {  
  2.   
  3.     private int count;  
  4.   
  5.     private int start;  
  6.   
  7.     private int total;  
  8.   
  9.     private String title;  
  10.   
  11.     private T subjects;  
  12.   
  13.     public int getCount() {  
  14.         return count;  
  15.     }  
  16.   
  17.     public void setCount(int count) {  
  18.         this.count = count;  
  19.     }  
  20.   
  21.     public int getStart() {  
  22.         return start;  
  23.     }  
  24.   
  25.     public void setStart(int start) {  
  26.         this.start = start;  
  27.     }  
  28.   
  29.     public int getTotal() {  
  30.         return total;  
  31.     }  
  32.   
  33.     public void setTotal(int total) {  
  34.         this.total = total;  
  35.     }  
  36.   
  37.     public String getTitle() {  
  38.         return title;  
  39.     }  
  40.   
  41.     public void setTitle(String title) {  
  42.         this.title = title;  
  43.     }  
  44.   
  45.     public T getSubjects() {  
  46.         return subjects;  
  47.     }  
  48.   
  49.     public void setSubjects(T subjects) {  
  50.         this.subjects = subjects;  
  51.     }  
  52. }  

Subject.java

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class Subject {  
  2.   
  3.     private String id;  
  4.     private String alt;  
  5.     private String year;  
  6.     private String title;  
  7.     private String original_title;  
  8.     private List<String> genres;  
  9.     private List<Cast> casts;  
  10.     private List<Cast> directors;  
  11.     private Avatars images;  
  12.   
  13.     @Override  
  14.     public String toString() {  
  15.         return "Subject.id=" + id  
  16.                 + " Subject.title=" + title  
  17.                 + " Subject.year=" + year  
  18.                 + " Subject.originalTitle=" + original_title + casts.toString() + directors.toString() + " | ";  
  19.     }  
  20.   
  21.     public String getId() {  
  22.         return id;  
  23.     }  
  24.   
  25.     public void setId(String id) {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     public String getAlt() {  
  30.         return alt;  
  31.     }  
  32.   
  33.     public void setAlt(String alt) {  
  34.         this.alt = alt;  
  35.     }  
  36.   
  37.     public String getYear() {  
  38.         return year;  
  39.     }  
  40.   
  41.     public void setYear(String year) {  
  42.         this.year = year;  
  43.     }  
  44.   
  45.     public String getTitle() {  
  46.         return title;  
  47.     }  
  48.   
  49.     public void setTitle(String title) {  
  50.         this.title = title;  
  51.     }  
  52.   
  53.     public String getOriginal_title() {  
  54.         return original_title;  
  55.     }  
  56.   
  57.     public void setOriginal_title(String original_title) {  
  58.         this.original_title = original_title;  
  59.     }  
  60.   
  61.     public List<String> getGenres() {  
  62.         return genres;  
  63.     }  
  64.   
  65.     public void setGenres(List<String> genres) {  
  66.         this.genres = genres;  
  67.     }  
  68.   
  69.     public List<Cast> getCasts() {  
  70.         return casts;  
  71.     }  
  72.   
  73.     public void setCasts(List<Cast> casts) {  
  74.         this.casts = casts;  
  75.     }  
  76.   
  77.     public List<Cast> getDirectors() {  
  78.         return directors;  
  79.     }  
  80.   
  81.     public void setDirectors(List<Cast> directors) {  
  82.         this.directors = directors;  
  83.     }  
  84.   
  85.     public Avatars getImages() {  
  86.         return images;  
  87.     }  
  88.   
  89.     public void setImages(Avatars images) {  
  90.         this.images = images;  
  91.     }  
  92.   
  93.     private class Cast {  
  94.         private String id;  
  95.         private String name;  
  96.         private String alt;  
  97.         private Avatars avatars;  
  98.   
  99.         public String getId() {  
  100.             return id;  
  101.         }  
  102.   
  103.         public void setId(String id) {  
  104.             this.id = id;  
  105.         }  
  106.   
  107.         public String getName() {  
  108.             return name;  
  109.         }  
  110.   
  111.         public void setName(String name) {  
  112.             this.name = name;  
  113.         }  
  114.   
  115.         public String getAlt() {  
  116.             return alt;  
  117.         }  
  118.   
  119.         public void setAlt(String alt) {  
  120.             this.alt = alt;  
  121.         }  
  122.   
  123.         public Avatars getAvatars() {  
  124.             return avatars;  
  125.         }  
  126.   
  127.         public void setAvatars(Avatars avatars) {  
  128.             this.avatars = avatars;  
  129.         }  
  130.   
  131.         @Override  
  132.         public String toString() {  
  133.             return "cast.id=" + id + " cast.name=" + name + " | ";  
  134.         }  
  135.     }  
  136.   
  137.     private class Avatars {  
  138.         private String small;  
  139.         private String medium;  
  140.         private String large;  
  141.   
  142.         public String getSmall() {  
  143.             return small;  
  144.         }  
  145.   
  146.         public void setSmall(String small) {  
  147.             this.small = small;  
  148.         }  
  149.   
  150.         public String getMedium() {  
  151.             return medium;  
  152.         }  
  153.   
  154.         public void setMedium(String medium) {  
  155.             this.medium = medium;  
  156.         }  
  157.   
  158.         public String getLarge() {  
  159.             return large;  
  160.         }  
  161.   
  162.         public void setLarge(String large) {  
  163.             this.large = large;  
  164.         }  
  165.     }  
  166. }  

用到的依赖

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. dependencies {  
  2.     compile fileTree(dir: 'libs', include: ['*.jar'])  
  3.     testCompile 'junit:junit:4.12'  
  4.     compile 'com.android.support:appcompat-v7:23.4.0'  
  5.     compile 'io.reactivex:rxjava:1.1.0'  
  6.     compile 'io.reactivex:rxandroid:1.1.0'  
  7.     compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'  
  8.     compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'  
  9.     compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'  
  10.     compile 'com.google.code.gson:gson:2.6.2'  
  11.     compile 'com.jakewharton:butterknife:7.0.1'  
  12. }  

 

参考链接:

http://gank.io/post/56e80c2c677659311bed9841


DEMO下载

posted @ 2017-04-26 12:57  天涯海角路  阅读(205)  评论(0)    收藏  举报