Search in sources :

Example 1 with StringConvert

use of com.lzy.okgo.convert.StringConvert in project okhttp-OkGo by jeasonlzy.

the class UploadAdapter method updateData.

public void updateData(int type) {
    // 这里是将数据库的数据恢复
    this.type = type;
    if (type == TYPE_ALL)
        values = OkUpload.restore(UploadManager.getInstance().getAll());
    if (type == TYPE_FINISH)
        values = OkUpload.restore(UploadManager.getInstance().getFinished());
    if (type == TYPE_ING)
        values = OkUpload.restore(UploadManager.getInstance().getUploading());
    // 至于数据类型,统一就行,不一定非要是String
    for (UploadTask<?> task : values) {
        // noinspection unchecked
        Request<String, ? extends Request> request = (Request<String, ? extends Request>) task.progress.request;
        request.converter(new StringConvert());
    }
    notifyDataSetChanged();
}
Also used : StringConvert(com.lzy.okgo.convert.StringConvert) Request(com.lzy.okgo.request.base.Request) PostRequest(com.lzy.okgo.request.PostRequest)

Example 2 with StringConvert

use of com.lzy.okgo.convert.StringConvert in project okhttp-OkGo by jeasonlzy.

the class UploadAdapter method updateData.

public List<UploadTask<?>> updateData(List<ImageItem> images) {
    this.type = -1;
    this.images = images;
    values = new ArrayList<>();
    if (images != null) {
        Random random = new Random();
        for (int i = 0; i < images.size(); i++) {
            ImageItem imageItem = images.get(i);
            // 这里是演示可以传递任何数据
            PostRequest<String> postRequest = // 
            OkGo.<String>post(Urls.URL_FORM_UPLOAD).headers("aaa", // 
            "111").params("bbb", // 
            "222").params("fileKey" + i, // 
            new File(imageItem.path)).converter(new StringConvert());
            UploadTask<String> task = // 
            OkUpload.request(imageItem.path, postRequest).priority(// 
            random.nextInt(100)).extra1(// 
            imageItem).save();
            values.add(task);
        }
    }
    notifyDataSetChanged();
    return values;
}
Also used : StringConvert(com.lzy.okgo.convert.StringConvert) Random(java.util.Random) ImageItem(com.lzy.imagepicker.bean.ImageItem) File(java.io.File)

Example 3 with StringConvert

use of com.lzy.okgo.convert.StringConvert in project okhttp-OkGo by jeasonlzy.

the class SyncActivity method sync.

@OnClick(R.id.sync)
public void sync(View view) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                // //同步会阻塞主线程,必须开线程
                // Response response = OkGo.get(Urls.URL_JSONOBJECT)//
                // .tag(this)//
                // .headers("header1", "headerValue1")//
                // .params("param1", "paramValue1")//
                // .execute();  //不传callback即为同步请求
                Call<String> call = // 
                OkGo.<String>get(Urls.URL_JSONOBJECT).tag(// 
                this).headers("header1", // 
                "headerValue1").params("param1", // 
                "paramValue1").converter(// 
                new StringConvert()).adapt();
                Response<String> response = call.execute();
                Message message = Message.obtain();
                message.obj = response.body();
                handler.sendMessage(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
Also used : StringConvert(com.lzy.okgo.convert.StringConvert) Message(android.os.Message) OnClick(butterknife.OnClick)

Example 4 with StringConvert

use of com.lzy.okgo.convert.StringConvert 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 5 with StringConvert

use of com.lzy.okgo.convert.StringConvert 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

StringConvert (com.lzy.okgo.convert.StringConvert)5 OnClick (butterknife.OnClick)3 Response (com.lzy.okgo.model.Response)2 ObservableResponse (com.lzy.okrx2.adapter.ObservableResponse)2 Disposable (io.reactivex.disposables.Disposable)2 File (java.io.File)2 Message (android.os.Message)1 LzyResponse (com.lzy.demo.model.LzyResponse)1 ImageItem (com.lzy.imagepicker.bean.ImageItem)1 PostRequest (com.lzy.okgo.request.PostRequest)1 Request (com.lzy.okgo.request.base.Request)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 JSONObject (org.json.JSONObject)1