okhttp3+retrofit2+rxjava2

依赖:
[java]  view plain  copy
  1. //retrofit2  
  2. implementation 'com.squareup.retrofit2:retrofit:2.4.0'  
  3. implementation 'com.squareup.retrofit2:converter-gson:2.4.0'  
  4. implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'  
  5. //rxjava2  
  6. implementation 'io.reactivex.rxjava2:rxjava:2.1.14-RC1'  
  7. implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'  
  8. //okhttp3  
  9. implementation 'com.squareup.okhttp3:okhttp:3.10.0'  
  10. implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'  
权限:
[html]  view plain  copy
  1. <uses-permission android:name="android.permission.INTERNET"/>  
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  

RetrofitInterface:

[java]  view plain  copy
  1. import com.example.rk7.model.bean.JavaBean;  
  2.   
  3. import io.reactivex.Observable;  
  4. import retrofit2.http.GET;  
  5. import retrofit2.http.Query;  
  6.   
  7. /** 
  8.  * author:Created by WangZhiQiang on 2018/5/16. 
  9.  */  
  10. public interface RetrofitInterface {  
  11.     @GET("nba/?key=71e58b5b2f930eaf1f937407acde08fe&")  
  12.     Observable<JavaBean> getCall(@Query("num"int num);  
  13. }  

RetrofitUtil:(Retrofit的简单封装类)

[java]  view plain  copy
  1. import android.util.Log;  
  2.   
  3. import com.example.rk7.model.callback.RetrofitInterface;  
  4.   
  5. import java.io.IOException;  
  6. import okhttp3.Interceptor;  
  7. import okhttp3.OkHttpClient;  
  8. import okhttp3.Request;  
  9. import okhttp3.logging.HttpLoggingInterceptor;  
  10. import retrofit2.Retrofit;  
  11. import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;  
  12. import retrofit2.converter.gson.GsonConverterFactory;  
  13.   
  14. /** 
  15.  * author:Created by WangZhiQiang on 2018/5/16. 
  16.  */  
  17. public class RetrofitUtil{  
  18.     private Retrofit retrofit;  
  19.     private static RetrofitUtil retrofitUtil;  
  20.   
  21.     private RetrofitUtil() { }  
  22.     private RetrofitUtil(String baseUrl) {  
  23.         //第三方的日志拦截器  
  24.         HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());  
  25.         logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);  
  26.         //OKhttp3  设置拦截器打印日志  
  27.         OkHttpClient okHttpClient = new OkHttpClient().newBuilder()  
  28. //                .addNetworkInterceptor(logInterceptor)  
  29. //                .addInterceptor(new LoggingInterceptor())//自定义拦截器  
  30.                 .build();  
  31.         retrofit = new Retrofit.Builder()  
  32.                 .baseUrl(baseUrl) //设置网络请求的Url地址  
  33.                 .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器  
  34.                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//支持RxJava2平台  
  35.                 .client(okHttpClient)//OKhttp3添加到Retrofit  
  36.                 .build();  
  37.     }  
  38.     //可指定baseUrl  
  39.     public static RetrofitUtil getInstance(String baseUrl){  
  40.         if (retrofitUtil==null){  
  41.             synchronized (RetrofitUtil.class){  
  42.                 if (null==retrofitUtil){  
  43.                     retrofitUtil = new RetrofitUtil(baseUrl);  
  44.                 }  
  45.             }  
  46.         }  
  47.         return retrofitUtil;  
  48.     }  
  49.     //默认的baseUrl  
  50.     public static RetrofitUtil getInstance(){  
  51.         if (null == retrofitUtil){  
  52.             return  getInstance("http://api.tianapi.com/");  
  53.         }  
  54.         return retrofitUtil;  
  55.     }  
  56.     //获得Retrofit  
  57.     public Retrofit getRetrofit(){  
  58.         return retrofit;  
  59.     }  
  60.     //直接获得RetrofitInterface  
  61.     public RetrofitInterface getRetrofitInterface(){  
  62.         RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);  
  63.         return retrofitInterface;  
  64.     }  
  65.     //自定义拦截器  
  66.     public static class LoggingInterceptor implements Interceptor {  
  67.         @Override public okhttp3.Response intercept(Chain chain) throws IOException {  
  68.             Request request = chain.request();  
  69.             //请求前--打印请求信息  
  70.             long t1 = System.nanoTime();  
  71.             Log.e("myMessage",String.format("Sending url %s \n %s \n\n%s \n\n %s",  
  72.                     request.url(), chain.connection(), request.headers(),request.body()));  
  73.   
  74.             //网络请求  
  75.             okhttp3.Response response = chain.proceed(request);  
  76.   
  77.             //网络响应后--打印响应信息  
  78.             long t2 = System.nanoTime();  
  79.             Log.e("myMessage",String.format("Received response for %s in %.1fms%n%s",  
  80.                     response.request().url(), (t2 - t1) / 1e6d, response.headers()));  
  81.   
  82.             return response;  
  83.         }  
  84.     }  
  85.     //日志信息  
  86.     public static class HttpLogger implements HttpLoggingInterceptor.Logger {  
  87.         @Override  
  88.         public void log(String message) {  
  89.             Log.e("myMessage", message);  
  90.         }  
  91.     }  
  92. }  
使用:
[java]  view plain  copy
  1. import android.util.Log;  
  2.   
  3. import com.example.rk7.model.bean.JavaBean;  
  4. import com.example.rk7.model.http.RetrofitUtil;  
  5. import com.example.rk7.view.interfaces.IMainView;  
  6.   
  7. import io.reactivex.Observable;  
  8. import io.reactivex.android.schedulers.AndroidSchedulers;  
  9. import io.reactivex.functions.Consumer;  
  10. import io.reactivex.schedulers.Schedulers;  
  11.   
  12. /** 
  13.  * author:Created by WangZhiQiang on 2018/4/16. 
  14.  */  
  15. public class MainPresenter extends BasePresenter<IMainView>{  
  16.     public void getDataFromServer(int num) {  
  17. //        Retrofit retrofit = RetrofitUtil.getInstance().getRetrofit();  
  18. //        RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);  
  19.         Observable<JavaBean> observable = RetrofitUtil.getInstance().getRetrofitInterface().getCall(num);  
  20.         observable.subscribeOn(Schedulers.io())  
  21.                 .observeOn(AndroidSchedulers.mainThread())  
  22.                 .subscribe(new Consumer<JavaBean>() {  
  23.                     @Override  
  24.                     public void accept(JavaBean javaBean) throws Exception {  
  25.                         Log.e("getDataFromServer",javaBean.getNewslist().size()+"");  
  26.                     }  
  27.                 });  
  28.         //map操作符使用  
  29. //        Observable<JavaBean> observable = RetrofitUtil.getInstance().getRetrofitInterface().getCall(num);  
  30. //        observable.subscribeOn(Schedulers.io())  
  31. //                .observeOn(AndroidSchedulers.mainThread())  
  32. //                .map(new Function<JavaBean, JavaBean.NewslistBean>() {  
  33. //                    @Override  
  34. //                    public JavaBean.NewslistBean apply(JavaBean javaBean) throws Exception {  
  35. //                        return javaBean.getNewslist().get(0);  
  36. //                    }  
  37. //                }).subscribe(new Consumer<JavaBean.NewslistBean>() {  
  38. //            @Override  
  39. //            public void accept(JavaBean.NewslistBean newslistBean) throws Exception {  
  40. //                Log.e("getDataFromServer",newslistBean.getTitle()+"");  
  41. //            }  
  42. //        });  
  43.     }  
  44. }  
JavaBean:
[java]  view plain  copy
  1. public class JavaBean {  
  2.     /** 
  3.      * code : 200 
  4.      * msg : success 
  5.      * newslist : [{"ctime":"2016-12-04 13:00","title":"格林:理解科尔吸食大麻 不过我从没吸过","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161204/1-161204120131.jpg","url":"http://www.51tyw.com/nba/2421.html"},{"ctime":"2016-12-04 00:00","title":"三分纪录延续!火箭队连续19场比赛命中10+三分球","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203233J3.jpg","url":"http://www.51tyw.com/nba/2417.html"},{"ctime":"2016-12-04 00:00","title":"詹姆斯谈三连败:是时候紧起来了 必须打得男人点","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203234010.jpg","url":"http://www.51tyw.com/nba/2418.html"},{"ctime":"2016-12-03 12:00","title":"骑士输赢都靠三分?那还要詹姆斯做什么?","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203104344.jpg","url":"http://www.51tyw.com/nba/2407.html"},{"ctime":"2016-12-03 00:00","title":"公牛vs骑士直播看点:詹伟兄弟对决","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202221205.jpg","url":"http://www.51tyw.com/nba/2395.html"},{"ctime":"2016-12-02 22:00","title":"火箭和勇士联手创NBA三分纪录","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202214212.jpg","url":"http://www.51tyw.com/nba/2392.html"},{"ctime":"2016-12-02 22:00","title":"巴克利:勇士打得像女式篮球,太软了!","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202215032.jpg","url":"http://www.51tyw.com/nba/2394.html"},{"ctime":"2016-12-02 20:00","title":"詹姆斯完成月最佳球员4连霸的壮举!","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-1612021PI0.jpg","url":"http://www.51tyw.com/nba/2388.html"},{"ctime":"2016-12-01 00:00","title":"威少再下三双战书!詹皇会不会先认怂?","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161128/1-16112Q13251.jpg","url":"http://www.51tyw.com/nba/2374.html"},{"ctime":"2016-12-01 00:00","title":"骑士这样的防守想夺冠?也许卫冕只是空谈!","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161130/1-161130222229.jpg","url":"http://www.51tyw.com/nba/2375.html"},{"ctime":"2016-12-01 00:00","title":"2016-2017赛季达拉斯小牛队球员名单","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161130/1-161130223630H8.gif","url":"http://www.51tyw.com/nba/2376.html"},{"ctime":"2016-11-28 12:00","title":"勇士队第一中锋竟是杜兰特","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161128/1-16112Q10H7.jpg","url":"http://www.51tyw.com/nba/2348.html"},{"ctime":"2016-11-28 12:00","title":"三双之战谁怕谁!詹皇三双数据超威少","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161128/1-16112Q12440.jpg","url":"http://www.51tyw.com/nba/2349.html"},{"ctime":"2016-11-28 12:00","title":"队史第一人!杜兰特成首位砍下25+10+5+5的球员","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161128/1-16112Q13251.jpg","url":"http://www.51tyw.com/nba/2350.html"},{"ctime":"2016-11-28 00:00","title":"湖人vs老鹰分析:缺兵少将的湖人恐不敌老鹰","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161127/1-16112H24117.jpg","url":"http://www.51tyw.com/nba/2346.html"},{"ctime":"2016-11-27 22:00","title":"2016-2017赛季亚特兰大老鹰队球员名单","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161125/1-1611251R231A1.gif","url":"http://www.51tyw.com/nba/2344.html"},{"ctime":"2016-11-25 22:00","title":"科比退役赛视频:黑曼巴最后一役砍60分","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161125/1-1611251R231A1.gif","url":"http://www.51tyw.com/nba/2307.html"},{"ctime":"2016-11-25 20:00","title":"NBA官方承认尼克杨绝杀走步","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161125/1-1611251R231A1.gif","url":"http://www.51tyw.com/nba/2304.html"},{"ctime":"2016-11-25 18:00","title":"骑士若无詹皇只能垫底 去76人都能进总决赛","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161125/1-1611251J401.jpg","url":"http://www.51tyw.com/nba/2303.html"},{"ctime":"2016-11-25 02:00","title":"湖人队赛程:2016-2017洛杉矶湖人队赛程表","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161122/1-161122205542.jpg","url":"http://www.51tyw.com/nba/2299.html"}] 
  6.      */  
  7.   
  8.     private int code;  
  9.     private String msg;  
  10.     private List<NewslistBean> newslist;  
  11.   
  12.     public int getCode() {  
  13.         return code;  
  14.     }  
  15.   
  16.     public void setCode(int code) {  
  17.         this.code = code;  
  18.     }  
  19.   
  20.     public String getMsg() {  
  21.         return msg;  
  22.     }  
  23.   
  24.     public void setMsg(String msg) {  
  25.         this.msg = msg;  
  26.     }  
  27.   
  28.     public List<NewslistBean> getNewslist() {  
  29.         return newslist;  
  30.     }  
  31.   
  32.     public void setNewslist(List<NewslistBean> newslist) {  
  33.         this.newslist = newslist;  
  34.     }  
  35.   
  36.     public static class NewslistBean {  
  37.         /** 
  38.          * ctime : 2016-12-04 13:00 
  39.          * title : 格林:理解科尔吸食大麻 不过我从没吸过 
  40.          * description : NBA新闻 
  41.          * picUrl : http://www.51tyw.com/uploads/allimg/161204/1-161204120131.jpg 
  42.          * url : http://www.51tyw.com/nba/2421.html 
  43.          */  
  44.   
  45.         private String ctime;  
  46.         private String title;  
  47.         private String description;  
  48.         private String picUrl;  
  49.         private String url;  
  50.   
  51.         public String getCtime() {  
  52.             return ctime;  
  53.         }  
  54.   
  55.         public void setCtime(String ctime) {  
  56.             this.ctime = ctime;  
  57.         }  
  58.   
  59.         public String getTitle() {  
  60.             return title;  
  61.         }  
  62.   
  63.         public void setTitle(String title) {  
  64.             this.title = title;  
  65.         }  
  66.   
  67.         public String getDescription() {  
  68.             return description;  
  69.         }  
  70.   
  71.         public void setDescription(String description) {  
  72.             this.description = description;  
  73.         }  
  74.   
  75.         public String getPicUrl() {  
  76.             return picUrl;  
  77.         }  
  78.   
  79.         public void setPicUrl(String picUrl) {  
  80.             this.picUrl = picUrl;  
  81.         }  
  82.   
  83.         public String getUrl() {  
  84.             return url;  
  85.         }  
  86.   
  87.         public void setUrl(String url) {  
  88.             this.url = url;  
  89.         }  
  90.     }  
  91. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值