MVP+Retrofit+Okhttp+RxJava

App的build.gradle依赖包:

         

[java] view plain copy
print ?
  1. compile ‘com.squareup.retrofit2:retrofit:2.0.0-beta4’//Retrofit2所需要的包  
  2. compile ’com.squareup.retrofit2:converter-gson:2.0.0-beta4’//ConverterFactory的Gson依赖包  
  3. compile ’com.squareup.retrofit2:converter-scalars:2.0.0-beta4’//ConverterFactory的String依赖包  
  4. compile ’com.facebook.fresco:fresco:0.11.0’  
  5. compile ’io.reactivex.rxjava2:rxjava:2.0.1’  
  6. compile ’io.reactivex.rxjava2:rxandroid:2.0.1’  
  7. compile ’com.squareup.retrofit2:adapter-rxjava2:2.3.0’  
     

接口地址类:

[java] view plain copy
print ?
  1. public class API {  
  2.   
  3.     //接口:APIKEY=‘71e58b5b2f930eaf1f937407acde08fe’     http://api.tianapi.com/nba/?key=APIKEY&num=10  
  4.     public static final String HOST=“http://api.tianapi.com/nba/”;  
  5.   
  6.     //接口:http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage  
  7.     public static final String BASE_URL= “http://result.eolinker.com/”;  
  8.     public static final String TOKEN= “umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611”;  
  9. }  
 


[java] view plain copy
print ?
  1. public interface ServiceAPI {  
  2.         @GET(API.HOST)  
  3.    //   Call  getNews(@Query(“key”)String n,@Query(“num”) int i);  
  4.         public Flowable getNews(@Query(“key”)String n,@Query(“num”int i);  
  5.   
  6.   
  7.       //  @GET(“{token}”)  
  8.      //   public Flowable> homepage(@Path(“token”) String token, @Query(“uri”) String uri);  
  9. }  
 

对Retrofit的封装:

[java] view plain copy
print ?
  1. public class RetrofitHelper {  
  2.  private   static OkHttpClient client;  
  3.   private   static ServiceAPI serviceAPI;  
  4.   
  5.     static {  
  6.           getClient();  
  7.     }  
  8.     public static OkHttpClient getClient(){  
  9.         if(client==null){  
  10.             synchronized (OkHttpClient.class){  
  11.                 if(client==null){  
  12.                     client=new OkHttpClient();  
  13.                 }  
  14.             }  
  15.         }  
  16.         return client;  
  17.     }  
  18.   
  19.     public static ServiceAPI getServiceAPI(){  
  20.         if(serviceAPI==null){  
  21.             synchronized (ServiceAPI.class){  
  22.                 if(serviceAPI==null){  
  23.                      serviceAPI=onCreate(ServiceAPI.class,API.HOST);  
  24.                 }  
  25.             }  
  26.         }  
  27.         return serviceAPI;  
  28.     }  
  29.   
  30.   
  31.     public static   T onCreate(Class tclass, String url){  
  32.   
  33.         Retrofit build = new Retrofit.Builder()  
  34.                 .baseUrl(url)  
  35.                 .addConverterFactory(GsonConverterFactory.create())  
  36.                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())  
  37.                 .client(client)  
  38.                 .build();  
  39.   
  40.        return build.create(tclass);  
  41.     }  
  42. }  
 

封装Bean类省略

MVP

Model层:

[java] view plain copy
print ?
  1. public interface CGSB {  
  2.     void chengGong(T t);  
  3.     void shiBai(Call call);  
  4. }  
 


[java] view plain copy
print ?
  1. public interface NewsModelJK {  
  2.    void getNews(String url, int i, CGSB cgsb);  
  3. }  
 
[java] view plain copy
print ?
  1. public class NewsModel implements NewsModelJK {  
  2.   
  3.   
  4.     public void getNews(String url, int i, final CGSB cgsb){  
  5.   
  6.         Flowable obserable = RetrofitHelper.getServiceAPI().getNews(url, i);//被观察者  
  7.   
  8.         obserable.subscribeOn(Schedulers.io())//被观察者       Scheduler (调度器)  
  9.   
  10.                   .observeOn(AndroidSchedulers.mainThread())       //观察者 切换到主线程  
  11.                   .subscribe(new Consumer() {  
  12.                       @Override  
  13.                       public void accept(NewsBean newsBean) throws Exception {  
  14.                           //调用成功的方法  
  15.                           cgsb.chengGong(newsBean);  
  16.                       }  
  17.                   });  
  18.   
  19.     }  
  20. }  
 

P层:

 

[java] view plain copy
print ?
  1. public interface INews {  
  2.     void showBean(NewsBean newsBean);  
  3. }  
 

[java] view plain copy
print ?
  1. public class NewsPersenter {  
  2.   
  3.     private final NewsModel newsModel;  
  4.     private INews iNews;  
  5.     public NewsPersenter(INews iNews) {  
  6.         newsModel = new NewsModel();  
  7.         this.iNews=iNews;  
  8.     }  
  9.     public  void getNews(final String n, int i){  
  10.         newsModel.getNews(n, i, new CGSB() {  
  11.             @Override  
  12.             public void chengGong(NewsBean newsBean) {  
  13.                 iNews.showBean(newsBean);  
  14.             }  
  15.   
  16.             @Override  
  17.             public void shiBai(Call call) {  
  18.   
  19.             }  
  20.         });  
  21.     }  
  22. }  
 

[java] view plain copy
print ?
  1. public class MainActivity extends AppCompatActivity implements INews {  
  2.   
  3.     private XRecyclerView mXrlv;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         initView();  
  10.         NewsPersenter newsPersenter = new NewsPersenter(this);  
  11.         newsPersenter.getNews(”71e58b5b2f930eaf1f937407acde08fe”10);  
  12.         /** 
  13.          * 沉浸式 
  14.          */  
  15.         initState();  
  16.   
  17.   
  18.     }  
  19.   
  20.     @Override  
  21.     public void showBean(NewsBean newsBean) {  
  22.    //     Log.i(“Tag”, newsBean.getMsg());  
  23.         List newslist = newsBean.getNewslist();  
  24.         mXrlv.setLayoutManager(new LinearLayoutManager(this));  
  25.         MyAdapter myAdapter = new MyAdapter(newslist, this);  
  26.         mXrlv.setAdapter(myAdapter);  
  27.   
  28.     }  
  29.   
  30.     private void initView() {  
  31.         mXrlv = (XRecyclerView) findViewById(R.id.xrlv);  
  32.   
  33.     }  
  34.     /** 
  35.      * 沉浸式状态栏 
  36.      */  
  37.     private void initState() {  
  38.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  39.             //透明状态栏  
  40.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  41.             //透明导航栏  
  42.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
  43.         }  
  44.     }  
  45. }  
 



你可能感兴趣的:(android控件)