OKHTTP

OKHTTP

1. OKHTTP Get请求

Request request = new Request.Builder().url(urlAddress).build();
request.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("lsw", "get请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) {
                Log.i("lsw", "get请求失败");
            }
        });

2. OKHTTP Post请求


public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
// 放入String类型的json数据, 通过create方法可以转换为标准json数据
RequestBody body = RequestBody.create(JSON, value);
Request request = new Request.Builder().url(urlAddress).post(body).addHeader("Content-Type", "application/json;charset=utf-8").build();
request.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("lsw", "post请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) {
                Log.i("lsw", "post请求失败");
            }
        });

3. OKHTTP 监听进度

相应的监听函数在okhttp工程中,下面的进度监听回调中,都是在UI线程中,因此可以直接在回调函数中更新UI
github地址


ProgressHelper.addProgressResponseListener(clickOKHttpClient, new UIProgressListener() {
                    @Override
                    public void onUIProgress(long currentBytes, long contentLength, boolean done) {
                        Log.i("lsw", "下载更新进度");
                        int process = (int) ((100 * currentBytes) / contentLength);
                        if (contentLength != -1) {
                            //长度未知的情况下回返回-1
                            Log.e("TAG", (100 * currentBytes) / contentLength + "% done");
                        }
                    }

                    @Override
                    public void onUIFinish(long currentBytes, long contentLength, boolean done) {
                        super.onUIFinish(currentBytes, contentLength, done);
                       // 监听结束时, 这与实际的完成情况还是有点差距的,如果要做到完全正确的监听,可以在下载完成以后,进行相应的UI操作
                    }

                    @Override
                    public void onUIStart(long currentBytes, long contentLength, boolean done) {
                        super.onUIStart(currentBytes, contentLength, done);
                        // 监听开始时
                    }
                }
        ).newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("lsw", "文件下载失败");
            }

            @Override
            public void onResponse(Call call, Response response) {
            Log.i("lsw", "文件下载成功");
               }
           }
        });

扩展链接

扩展链接1
扩展链接2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值