Search in sources :

Example 1 with ObservableResponse

use of com.lzy.okrx2.adapter.ObservableResponse in project okhttp-OkGo by jeasonlzy.

the class RxCommonActivity method upJson.

@OnClick(R.id.upJson)
public void upJson(View view) {
    HashMap<String, String> params = new HashMap<>();
    params.put("key1", "value1");
    params.put("key2", "这里是需要提交的json格式数据");
    params.put("key3", "也可以使用三方工具将对象转成json字符串");
    params.put("key4", "其实你怎么高兴怎么写都行");
    JSONObject jsonObject = new JSONObject(params);
    // 
    OkGo.<String>post(Urls.URL_TEXT_UPLOAD).headers("aaa", // 
    "111").upJson(// 
    jsonObject).converter(// 
    new StringConvert()).adapt(// 
    new ObservableResponse<String>()).subscribeOn(// 
    Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(@NonNull Disposable disposable) throws Exception {
            showLoading();
        }
    }).observeOn(// 
    AndroidSchedulers.mainThread()).subscribe(new Observer<Response<String>>() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            addDisposable(d);
        }

        @Override
        public void onNext(@NonNull Response<String> response) {
            handleResponse(response);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            e.printStackTrace();
            showToast("请求失败");
            handleError(null);
        }

        @Override
        public void onComplete() {
            dismissLoading();
        }
    });
}
Also used : StringConvert(com.lzy.okgo.convert.StringConvert) Disposable(io.reactivex.disposables.Disposable) HashMap(java.util.HashMap) Response(com.lzy.okgo.model.Response) ObservableResponse(com.lzy.okrx2.adapter.ObservableResponse) LzyResponse(com.lzy.demo.model.LzyResponse) JSONObject(org.json.JSONObject) OnClick(butterknife.OnClick)

Example 2 with ObservableResponse

use of com.lzy.okrx2.adapter.ObservableResponse in project okhttp-OkGo by jeasonlzy.

the class RxFormUploadActivity method formUpload1.

@OnClick(R.id.formUpload1)
public void formUpload1(View view) {
    ArrayList<File> files = new ArrayList<>();
    if (imageItems != null && imageItems.size() > 0) {
        for (int i = 0; i < imageItems.size(); i++) {
            files.add(new File(imageItems.get(i).path));
        }
    }
    // 拼接参数
    // 
    OkGo.<String>post(Urls.URL_FORM_UPLOAD).tag(// 
    this).headers("header1", // 
    "headerValue1").headers("header2", // 
    "headerValue2").params("param1", // 
    "paramValue1").params("param2", // 
    "paramValue2").addFileParams("file", // 
    files).converter(// 
    new StringConvert()).adapt(// 
    new ObservableResponse<String>()).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(@NonNull Disposable disposable) throws Exception {
            btnFormUpload1.setText("正在上传中...\n使用Rx方式做进度监听稍显麻烦,推荐使用方式2");
        }
    }).observeOn(// 
    AndroidSchedulers.mainThread()).subscribe(new Observer<Response<String>>() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            addDisposable(d);
        }

        @Override
        public void onNext(@NonNull Response<String> response) {
            btnFormUpload1.setText("上传完成");
            handleResponse(response);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            e.printStackTrace();
            btnFormUpload1.setText("上传出错");
            showToast(e.getMessage());
            handleError(null);
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : StringConvert(com.lzy.okgo.convert.StringConvert) Disposable(io.reactivex.disposables.Disposable) ArrayList(java.util.ArrayList) Response(com.lzy.okgo.model.Response) ObservableResponse(com.lzy.okrx2.adapter.ObservableResponse) ObservableResponse(com.lzy.okrx2.adapter.ObservableResponse) File(java.io.File) OnClick(butterknife.OnClick)

Aggregations

OnClick (butterknife.OnClick)2 StringConvert (com.lzy.okgo.convert.StringConvert)2 Response (com.lzy.okgo.model.Response)2 ObservableResponse (com.lzy.okrx2.adapter.ObservableResponse)2 Disposable (io.reactivex.disposables.Disposable)2 LzyResponse (com.lzy.demo.model.LzyResponse)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1