Search in sources :

Example 6 with ServerModel

use of com.lzy.demo.model.ServerModel in project okhttp-OkGo by jeasonlzy.

the class RxCommonActivity method jsonRequest.

@OnClick(R.id.jsonRequest)
public void jsonRequest(View view) {
    Subscription subscription = //
    OkGo.post(Urls.URL_JSONOBJECT).headers("aaa", //
    "111").params("bbb", //一定要注意这里的写法,JsonConvert最后的大括号千万不能忘记
    "222").getCall(new JsonConvert<LzyResponse<ServerModel>>() {
    }, //
    RxAdapter.<LzyResponse<ServerModel>>create()).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            showLoading();
        }
    }).map(new Func1<LzyResponse<ServerModel>, ServerModel>() {

        @Override
        public ServerModel call(LzyResponse<ServerModel> response) {
            return response.data;
        }
    }).observeOn(//
    AndroidSchedulers.mainThread()).subscribe(new Action1<ServerModel>() {

        @Override
        public void call(ServerModel serverModel) {
            //请求成功
            dismissLoading();
            handleResponse(serverModel, null, null);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            //请求失败
            throwable.printStackTrace();
            showToast("请求失败");
            dismissLoading();
            handleError(null, null);
        }
    });
    addSubscribe(subscription);
}
Also used : LzyResponse(com.lzy.demo.model.LzyResponse) Action0(rx.functions.Action0) ServerModel(com.lzy.demo.model.ServerModel) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Aggregations

OnClick (butterknife.OnClick)6 LzyResponse (com.lzy.demo.model.LzyResponse)6 ServerModel (com.lzy.demo.model.ServerModel)6 Subscription (rx.Subscription)3 Action0 (rx.functions.Action0)3 File (java.io.File)2 Call (okhttp3.Call)2 Response (okhttp3.Response)2 Bitmap (android.graphics.Bitmap)1 FileConvert (com.lzy.okgo.convert.FileConvert)1 BaseRequest (com.lzy.okgo.request.BaseRequest)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 JSONObject (org.json.JSONObject)1