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

来源:互联网 发布:云计算大数据什么意思 编辑:程序博客网 时间:2024/06/03 19:20

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

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

 1.需要的类库:

    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'    compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'    compile 'io.reactivex:rxjava:1.1.0'    compile 'io.reactivex:rxandroid:1.1.0'

2.GET形式:

 @GET("/weather")        WeatherData getWeather(@Query("APPID") String AppID,@Query("q") String place, @Query("units") String units);

3.POST形式:

 @FormUrlEncoded        @POST("/cuslogin")        Observable<Chni_User>  login (@Field("data") String data);

 4.Retrofit实现

 private static final Retrofit sRetrofit = new Retrofit .Builder()            .baseUrl(ENDPOINT)            .addConverterFactory(GsonConverterFactory.create())            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作为回调适配器            .build();    private static final ApiManagerService apiManager = sRetrofit.create(ApiManagerService.class);

5.以下是代码的实现

   private interface ApiManagerService {        @GET("/weather")        WeatherData getWeather(@Query("APPID") String AppID,@Query("q") String place, @Query("units") String units);        /**         * retrofit 支持 rxjava 整合         * 这种方法适用于新接口         */        @GET("/weather")        Observable<WeatherData> getWeatherData(@Query("APPID") String AppID,@Query("q") String place, @Query("units") String units);        @FormUrlEncoded        @POST("/cuslogin")        Observable<Chni_User>  login (@Field("data") String data);    }

/**     * 将服务接口返回的数据,封装成{@link rx.Observable}     * 这种写法适用于将旧代码封装     * @param city     * @return     */    public static Observable<WeatherData> getWeatherData(final String city) {        return Observable.create(new Observable.OnSubscribe<WeatherData>() {            @Override            public void call(Subscriber<? super WeatherData> subscriber) {                //订阅者回调 onNext 和 onCompleted                subscriber.onNext(apiManager.getWeather("ee9c42978f2e9be685994bf32b87cdc0", city, "metric"));                subscriber.onCompleted();            }        }).subscribeOn(Schedulers.io());    }    public static Observable<WeatherData> getWeatherData1(final String city) {        return apiManager.getWeatherData("ee9c42978f2e9be685994bf32b87cdc0", city, "metric");    }
public static Observable<Chni_User> login( final String data){//        return  apiManager.getMyProduces(pageNumber,"10");        return apiManager.login(data);    }

调用:
      String t = SystemAttrUtlis.getPostTimeNow();        String s = "0010000app";        String ko = "0000";        String time = t;        String token = MD5s(t + s);        String data = "";        Map<String, String> params = new HashMap<String, String>();        params.put("username", "lidong");        params.put("password", "325106");        params.put("orgid", "0010000");        CkeckServerTokenUtils.handleServerCheck(params);        final Map<String, String> params1 = new HashMap<String, String>();        JSONObject jsonObject1 = new  JSONObject(params);        data=jsonObject1.toString();        Log.v("zgy", data);        ApiManager.login(data).subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Action1<Chni_User>() {                    @Override                    public void call(Chni_User ss) {//                        Log.d(LOG_TAG, weatherData.toString());                        ((TextView) findViewById(R.id.tv_demo)).setText(ss.getCustomerid());                    }                }, new Action1<Throwable>() {                    @Override                    public void call(Throwable throwable) {                        Log.e(LOG_TAG, throwable.getMessage(), throwable);                    }                });    }
 ApiManager.getWeatherData1(CITIES[0]).subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Action1<WeatherData>() {                    @Override                    public void call(WeatherData weatherData) {                        Log.d(LOG_TAG, weatherData.toString());                       ((TextView) findViewById(R.id.tv_demo1)).setText(weatherData.toString());                    }                }, new Action1<Throwable>() {                    @Override                    public void call(Throwable throwable) {                        Log.e(LOG_TAG, throwable.getMessage(), throwable);                   }               });

就这些了吧。有问题大家提出来。
0 1