Retrofit+MVP

导入依赖
[java]  view plain  copy
  1. compile 'com.squareup.retrofit2:retrofit:2.0.0'  
  2. compile 'com.squareup.retrofit2:converter-gson:2.0.2'  
  3. compile 'com.google.code.gson:gson:2.8.2'  

实体类

[java]  view plain  copy
  1. public class Beannes<T> {  
  2.     private String code;  
  3.     private String msg;  
  4.     private T newslist;  
  5.   
  6.     public String getCode() {  
  7.         return code;  
  8.     }  
  9.   
  10.     public void setCode(String code) {  
  11.         this.code = code;  
  12.     }  
  13.   
  14.     public String getMsg() {  
  15.         return msg;  
  16.     }  
  17.   
  18.     public void setMsg(String msg) {  
  19.         this.msg = msg;  
  20.     }  
  21.   
  22.     public T getNewslist() {  
  23.         return newslist;  
  24.     }  
  25.   
  26.     public void setNewslist(T newslist) {  
  27.         this.newslist = newslist;  
  28.     }  
  29. }  
[java]  view plain  copy
  1. public class NewsBean {  
  2.      
  3.   
  4.     private String ctime;  
  5.     private String title;  
  6.     private String description;  
  7.     private String picUrl;  
  8.     private String url;  
  9.   
  10.     public String getCtime() {  
  11.         return ctime;  
  12.     }  
  13.   
  14.     public void setCtime(String ctime) {  
  15.         this.ctime = ctime;  
  16.     }  
  17.   
  18.     public String getTitle() {  
  19.         return title;  
  20.     }  
  21.   
  22.     public void setTitle(String title) {  
  23.         this.title = title;  
  24.     }  
  25.   
  26.     public String getDescription() {  
  27.         return description;  
  28.     }  
  29.   
  30.     public void setDescription(String description) {  
  31.         this.description = description;  
  32.     }  
  33.   
  34.     public String getPicUrl() {  
  35.         return picUrl;  
  36.     }  
  37.   
  38.     public void setPicUrl(String picUrl) {  
  39.         this.picUrl = picUrl;  
  40.     }  
  41.   
  42.     public String getUrl() {  
  43.         return url;  
  44.     }  
  45.   
  46.     public void setUrl(String url) {  
  47.         this.url = url;  
  48.     }  
  49. }  
M和P层接口

[java]  view plain  copy
  1. public interface CallBack {  
  2.     void onSuccess(Object o);  
  3.     void onFailed(Throwable t);  
  4. }  
P和V
[java]  view plain  copy
  1. public interface  IView {  
  2.     void success(Beannes<List<NewsBean>> data);  
  3.     void failed(Throwable e);  
  4. }  
Retrofit请求地址拼接接口
[java]  view plain  copy
  1. public interface RetrofitService {  
  2.     @HTTP(method = "GET", path = "nba", hasBody = false)  
  3.     Call<Beannes<List<NewsBean>>> getBlog(@Query("key") String ad1, @Query("num") String id2);  
  4. }  
RetrofitUtils
[java]  view plain  copy
  1. public class RetrofitUtils {//单例  
  2.     private static volatile RetrofitUtils instance;  
  3.     public static RetrofitUtils getInstance(){  
  4.             if (instance==null){  
  5.                 synchronized (RetrofitUtils.class){  
  6.                     if (instance==null){  
  7.                         instance=new RetrofitUtils();  
  8.                     }  
  9.                 }  
  10.             }  
  11.             return instance;  
  12.     }  
  13.     public void getNews(String baseurl, final CallBack callBack, String ad1, int ad2){  
  14.         Retrofit retrofit = new Retrofit.Builder()  
  15.                 .baseUrl(baseurl)  
  16.                 .addConverterFactory(GsonConverterFactory.create())  
  17.                 .build();  
  18.         RetrofitService retrofitService = retrofit.create(RetrofitService.class);  
  19.         Call<Beannes<List<NewsBean>>> call = retrofitService.getBlog(ad1, ad2+"");  
  20.         call.enqueue(new Callback<Beannes<List<NewsBean>>>() {  
  21.             @Override  
  22.             public void onResponse(Call<Beannes<List<NewsBean>>> call, Response<Beannes<List<NewsBean>>> response) {  
  23.                 Beannes<List<NewsBean>> body = response.body();  
  24.                 callBack.onSuccess(body);  
  25.             }  
  26.   
  27.             @Override  
  28.             public void onFailure(Call<Beannes<List<NewsBean>>> call, Throwable t) {  
  29.                 callBack.onFailed(t);  
  30.             }  
  31.         });  
  32.     }  
  33. }  
NewsPresenter

[java]  view plain  copy
  1. public class NewsPresenter {  
  2.     private IView iView;  
  3.   
  4.     public NewsPresenter() {  
  5.     }  
  6.   
  7.     public void attachView(IView iView) {  
  8.         this.iView = iView;  
  9.     }  
  10.   
  11.     public void getData(String url, String ad1, int ad2) {  
  12.         RetrofitUtils.getInstance().getNews(url, new CallBack() {  
  13.             @Override  
  14.             public void onSuccess(Object o) {  
  15.                 Beannes<List<NewsBean>> data = (Beannes<List<NewsBean>>) o;  
  16.                 iView.success(data);  
  17.             }  
  18.   
  19.             @Override  
  20.             public void onFailed(Throwable t) {  
  21.                 iView.failed(t);  
  22.             }  
  23.         }, ad1, ad2);  
  24.     }  
  25. }  
V层使用
[java]  view plain  copy
  1. private void getNews() {  
  2.        presenter = new NewsPresenter();  
  3.        presenter.attachView(this);  
  4.        presenter.getData("http://api.tianapi.com/""71e58b5b2f930eaf1f937407acde08fe", i);  
  5.    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值