Retrofit+RxJava返回结果处理

一. 想获取头部里的cookie信息

1.接口

public interface ApiService {

    String GET_LOGIN = "api/news/feed/v62/?refer=1&count=20&loc_mode=4&device_id=34960436458&iid=13136511752";

    @GET(GET_LOGIN)
    Observable> requestLogin(@Query("category") String category, @Query("min_behot_time") long lastTime, @Query("last_refresh_sub_entrance_interval") long currentTime);

}

2.请求

 public void requestLogin(String account) {
        long timstamp = System.currentTimeMillis() / 1000;
        Observable> observable = mApiService.requestLogin(account,timstamp,timstamp);
        addSubscription(observable, new Subscriber>() {
            @Override
            public void onCompleted() {

            }
            @Override
            public void onError(Throwable e) {

            }
            @Override
            public void onNext(Response response) {
                KLog.d("=======headers:" + response.headers().toString());
                KLog.d("=======body:" + response.body());

                /*
                * 这样可以得到头部里cookie信息
                * body是个string, 应为泛型传的是String, 也可以传入UserBean
                * */
            }
        });
    }

二. 直接获取body数据

接口
public interface ApiService {

    String GET_LOGIN = "api/news/feed/v62/?refer=1&count=20&loc_mode=4&device_id=34960436458&iid=13136511752";

    @GET(GET_LOGIN)
    Observable requestLogin(@Query("category") String category, @Query("min_behot_time") long lastTime, @Query("last_refresh_sub_entrance_interval") long currentTime);
    
}


请求
public void requestLogin(String account) {
        long timstamp = System.currentTimeMillis() / 1000;
        Observable observable = mApiService.requestLogin(account,timstamp,timstamp);
        addSubscription(observable, new Subscriber() {
            @Override
            public void onCompleted() {

            }
            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {

                KLog.d("=======body:" + s);
                /*
                * s 就是body数据
                * */
            }

        });
    }



Retrofit常用

    @FormUrlEncoded
    @POST("login")
    Observable login0(@FieldMap Map map);

    @POST("login")
    Observable login1(@Body NewsData data);

    @GET("public")
    Observable login2(@QueryMap Map map);
    
    @GET
    Observable login3(@Url String url, @Query("city") String city);

你可能感兴趣的:(Retrofit+RxJava返回结果处理)