Android Retrofit 2.0框架 GET和POST的实现方式(配合RxJava)

来源:互联网 发布:中国数据分析行业网 编辑:程序博客网 时间:2024/06/06 02:47

目录(?)[+]

  1. Android Retrofit 20框架 GET和POST的实现方式配合RxJava
    1.  1需要的类库
    2. GET形式
    3. POST形式
    4.  4Retrofit实现
    5. 以下是代码的实现

Android Retrofit 2.0框架 GET和POST的实现方式(配合RxJava)

  简单说两句,该框架是okhttp再次封装的实现,性能挺高的哦。不罗嗦了,直接上代码

 1.需要的类库:

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'  
  2. compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'  
  3. compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'  
  4. compile 'io.reactivex:rxjava:1.1.0'  
  5. compile 'io.reactivex:rxandroid:1.1.0'  

2.GET形式:

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. @GET("/weather")  
  2.        WeatherData getWeather(@Query("APPID") String AppID,@Query("q") String place, @Query("units") String units);  

3.POST形式:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. @FormUrlEncoded  
  2.        @POST("/cuslogin")  
  3.        Observable<Chni_User>  login (@Field("data") String data);  

 4.Retrofit实现

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. private static final Retrofit sRetrofit = new Retrofit .Builder()  
  2.            .baseUrl(ENDPOINT)  
  3.            .addConverterFactory(GsonConverterFactory.create())  
  4.            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作为回调适配器  
  5.            .build();  
  6.    private static final ApiManagerService apiManager = sRetrofit.create(ApiManagerService.class);  

5.以下是代码的实现

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. private interface ApiManagerService {  
  2.   
  3.      @GET("/weather")  
  4.      WeatherData getWeather(@Query("APPID") String AppID,@Query("q") String place, @Query("units") String units);  
  5.   
  6.      /** 
  7.       * retrofit 支持 rxjava 整合 
  8.       * 这种方法适用于新接口 
  9.       */  
  10.      @GET("/weather")  
  11.      Observable<WeatherData> getWeatherData(@Query("APPID") String AppID,@Query("q") String place, @Query("units") String units);  
  12.   
  13.      @FormUrlEncoded  
  14.      @POST("/cuslogin")  
  15.      Observable<Chni_User>  login (@Field("data") String data);  
  16.   
  17.   
  18.  }  

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 将服务接口返回的数据,封装成{@link rx.Observable} 
  3.      * 这种写法适用于将旧代码封装 
  4.      * @param city 
  5.      * @return 
  6.      */  
  7.     public static Observable<WeatherData> getWeatherData(final String city) {  
  8.         return Observable.create(new Observable.OnSubscribe<WeatherData>() {  
  9.             @Override  
  10.             public void call(Subscriber<? super WeatherData> subscriber) {  
  11.                 //订阅者回调 onNext 和 onCompleted  
  12.                 subscriber.onNext(apiManager.getWeather("ee9c42978f2e9be685994bf32b87cdc0", city, "metric"));  
  13.                 subscriber.onCompleted();  
  14.             }  
  15.         }).subscribeOn(Schedulers.io());  
  16.     }  
  17.   
  18.     public static Observable<WeatherData> getWeatherData1(final String city) {  
  19.         return apiManager.getWeatherData("ee9c42978f2e9be685994bf32b87cdc0", city, "metric");  
  20.     }  
[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1.   
[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. public static Observable<Chni_User> login( final String data){  
  2. //        return  apiManager.getMyProduces(pageNumber,"10");  
  3.         return apiManager.login(data);  
  4.     }  

调用:
[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1.       String t = SystemAttrUtlis.getPostTimeNow();  
  2.         String s = "0010000app";  
  3.         String ko = "0000";  
  4.         String time = t;  
  5.         String token = MD5s(t + s);  
  6.         String data = "";  
  7.         Map<String, String> params = new HashMap<String, String>();  
  8.         params.put("username""lidong");  
  9.         params.put("password""325106");  
  10.         params.put("orgid""0010000");  
  11.         CkeckServerTokenUtils.handleServerCheck(params);  
  12.         final Map<String, String> params1 = new HashMap<String, String>();  
  13.         JSONObject jsonObject1 = new  JSONObject(params);  
  14.         data=jsonObject1.toString();  
  15.         Log.v("zgy", data);  
  16.         ApiManager.login(data).subscribeOn(Schedulers.io())  
  17.                 .observeOn(AndroidSchedulers.mainThread())  
  18.                 .subscribe(new Action1<Chni_User>() {  
  19.                     @Override  
  20.                     public void call(Chni_User ss) {  
  21. //                        Log.d(LOG_TAG, weatherData.toString());  
  22.                         ((TextView) findViewById(R.id.tv_demo)).setText(ss.getCustomerid());  
  23.                     }  
  24.                 }, new Action1<Throwable>() {  
  25.                     @Override  
  26.                     public void call(Throwable throwable) {  
  27.                         Log.e(LOG_TAG, throwable.getMessage(), throwable);  
  28.                     }  
  29.                 });  
  30.     }  
[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. ApiManager.getWeatherData1(CITIES[0]).subscribeOn(Schedulers.io())  
  2.                .observeOn(AndroidSchedulers.mainThread())  
  3.                .subscribe(new Action1<WeatherData>() {  
  4.                    @Override  
  5.                    public void call(WeatherData weatherData) {  
  6.                        Log.d(LOG_TAG, weatherData.toString());  
  7.                       ((TextView) findViewById(R.id.tv_demo1)).setText(weatherData.toString());                    }  
  8.                }, new Action1<Throwable>() {                    @Override  
  9.                    public void call(Throwable throwable) {  
  10.                        Log.e(LOG_TAG, throwable.getMessage(), throwable);  
  11.                   }  
  12.               });  

就这些了吧。有问题大家提出来。
0 0
原创粉丝点击