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();
}
});
}
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() {
}
});
}
Aggregations