Android retrofit@body使い方
8555 ワード
Api
presenter
インタフェース要求インタフェース
@POST(" ")
Observable<PullBean> pull(@Body RequestBody body);
presenter
public void pull(String json){
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
Observable<PullBean> pull = okHttp.api.pull(requestBody);
pull.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<PullBean>() {
@Override
public void accept(PullBean pullBean) throws Exception {
if (getView() !=null){
getView().onSuccess(pullBean);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
getView().onError(throwable.toString());
}
});
}
インタフェース要求インタフェース
HashMap<String, String> map = new HashMap<String, String>();
map.put("ID" , "");
map.put("type" , "");
map.put("userID" , "");
gson = new Gson();
pull = gson.toJson(map);
presenter.pull(pull);