一. 想获取头部里的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<Response<String>> 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<Response<String>> observable = mApiService.requestLogin(account,timstamp,timstamp);
addSubscription(observable, new Subscriber<Response<String>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Response<String> 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<String> 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<String> observable = mApiService.requestLogin(account,timstamp,timstamp);
addSubscription(observable, new Subscriber<String>() {
@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<String> login0(@FieldMap Map<String, String> map);
@POST("login")
Observable<String> login1(@Body NewsData data);
@GET("public")
Observable<String> login2(@QueryMap Map<String,String> map);
@GET
Observable<String> login3(@Url String url, @Query("city") String city);