Search in sources :

Example 1 with ServerModel

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

the class FormUploadActivity method formUpload.

@OnClick(R.id.formUpload)
public void formUpload(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.post(Urls.URL_FORM_UPLOAD).tag(//
    this).headers("header1", //
    "headerValue1").headers("header2", //
    "headerValue2").params("param1", //
    "paramValue1").params("param2", //
    "paramValue2").addFileParams("file", // 这种方式为同一个key,上传多个文件
    files).execute(new JsonCallback<LzyResponse<ServerModel>>() {

        @Override
        public void onBefore(BaseRequest request) {
            super.onBefore(request);
            btnFormUpload.setText("正在上传中...");
        }

        @Override
        public void onSuccess(LzyResponse<ServerModel> responseData, Call call, Response response) {
            handleResponse(responseData.data, call, response);
            btnFormUpload.setText("上传完成");
        }

        @Override
        public void onError(Call call, Response response, Exception e) {
            super.onError(call, response, e);
            handleError(call, response);
            btnFormUpload.setText("上传出错");
        }

        @Override
        public void upProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
            System.out.println("upProgress -- " + totalSize + "  " + currentSize + "  " + progress + "  " + networkSpeed);
            String downloadLength = Formatter.formatFileSize(getApplicationContext(), currentSize);
            String totalLength = Formatter.formatFileSize(getApplicationContext(), totalSize);
            tvDownloadSize.setText(downloadLength + "/" + totalLength);
            String netSpeed = Formatter.formatFileSize(getApplicationContext(), networkSpeed);
            tvNetSpeed.setText(netSpeed + "/S");
            tvProgress.setText((Math.round(progress * 10000) * 1.0f / 100) + "%");
            pbProgress.setMax(100);
            pbProgress.setProgress((int) (progress * 100));
        }
    });
}
Also used : Call(okhttp3.Call) LzyResponse(com.lzy.demo.model.LzyResponse) ArrayList(java.util.ArrayList) Response(okhttp3.Response) LzyResponse(com.lzy.demo.model.LzyResponse) ServerModel(com.lzy.demo.model.ServerModel) BaseRequest(com.lzy.okgo.request.BaseRequest) File(java.io.File) OnClick(butterknife.OnClick)

Example 2 with ServerModel

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

the class TestActivity method btn1.

@OnClick(R.id.btn1)
public void btn1(View view) {
    Call<String> stringCall = OkGo.get("").getCall(StringConvert.create());
    Call<Bitmap> bitmapCall = OkGo.get("").getCall(BitmapConvert.create());
    Call<File> fileCall = OkGo.get("").getCall(new FileConvert());
    Call<LzyResponse<ServerModel>> call = OkGo.get("").getCall(new JsonConvert<LzyResponse<ServerModel>>() {
    });
    Call<LzyResponse<ServerModel>> listCall = OkGo.get("").getCall(new JsonConvert<LzyResponse<ServerModel>>() {
    });
    Observable<String> stringObservable = OkGo.get("").getCall(StringConvert.create(), RxAdapter.<String>create());
    Observable<LzyResponse<ServerModel>> observable = OkGo.get("").getCall(new JsonConvert<LzyResponse<ServerModel>>() {
    }, RxAdapter.<LzyResponse<ServerModel>>create());
}
Also used : LzyResponse(com.lzy.demo.model.LzyResponse) Bitmap(android.graphics.Bitmap) ServerModel(com.lzy.demo.model.ServerModel) FileConvert(com.lzy.okgo.convert.FileConvert) File(java.io.File) OnClick(butterknife.OnClick)

Example 3 with ServerModel

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

the class RxCommonActivity method jsonArrayRequest.

@OnClick(R.id.jsonArrayRequest)
public void jsonArrayRequest(View view) {
    Subscription subscription = //
    ServerApi.getServerListModel("aaa", "bbb").doOnSubscribe(new Action0() {

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

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

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

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

Example 4 with ServerModel

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

the class RxCommonActivity method retrofitRequest.

@OnClick(R.id.retrofitRequest)
public void retrofitRequest(View view) {
    Subscription subscription = //
    ServerApi.getServerModel("aaa", "bbb").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 : Action0(rx.functions.Action0) LzyResponse(com.lzy.demo.model.LzyResponse) ServerModel(com.lzy.demo.model.ServerModel) Subscription(rx.Subscription) OnClick(butterknife.OnClick)

Example 5 with ServerModel

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

the class PostTextActivity method postJson.

@OnClick(R.id.postJson)
public void postJson(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.post(Urls.URL_TEXT_UPLOAD).tag(//
    this).headers("header1", //
    "headerValue1").upJson(//
    jsonObject).execute(new DialogCallback<LzyResponse<ServerModel>>(this) {

        @Override
        public void onSuccess(LzyResponse<ServerModel> responseData, Call call, Response response) {
            handleResponse(responseData.data, call, response);
        }

        @Override
        public void onError(Call call, Response response, Exception e) {
            super.onError(call, response, e);
            handleError(call, response);
        }
    });
}
Also used : LzyResponse(com.lzy.demo.model.LzyResponse) Response(okhttp3.Response) Call(okhttp3.Call) LzyResponse(com.lzy.demo.model.LzyResponse) JSONObject(org.json.JSONObject) ServerModel(com.lzy.demo.model.ServerModel) HashMap(java.util.HashMap) 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