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

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);
                   }
               });


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值