OKHttp

okhttp的用途以及使用方法

1.GET请求方法

代码演示
//获取client对象
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.callTimeout(1, TimeUnit.MINUTES);
        builder.readTimeout(1, TimeUnit.MINUTES);
        OkHttpClient client = builder.build();
        //获取request对象
        Request.Builder builder1 = new Request.Builder();
        builder1.get();
        builder1.url("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703& itemid=2&act=ad_app");
        Request request = builder1.build();
        //call
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("wjx", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ResponseBody body = response.body();
                String json = body.string();
                Log.i("wjx", "onResponse: "+json);

            }
        });

2.POST请求方法

代码演示
//获取 client
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(1, TimeUnit.MINUTES)
                .callTimeout(1, TimeUnit.MINUTES)
                .build();
        //获取requestBody;
        FormBody formBody = new FormBody.Builder()
                .add("itemid", "2")
                .add("act", "ad_app")
                .build();
        //获取request
        final Request request = new Request.Builder()
                .url("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703")
                .post(formBody)
                .build();
        //获取call
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("wjx", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                String json = body.string();
                Log.i("wjx", "onResponse: "+json);

            }
        });

3.下载文件 使用GET请求

代码演示
 //获取 client
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(1, TimeUnit.MINUTES)
                .callTimeout(1, TimeUnit.MINUTES)
                .build();
        //获取request
        final Request request = new Request.Builder()
                .url("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4")
                .get()
                .build();
        //获取call
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("wjx", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                long l = body.contentLength();
                Log.i("wjx", "onResponse: "+l);
                InputStream inputStream = body.byteStream();
                FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/Movies/aa.mp4");
                int len = 0;
                int count = 0;
                byte[] b = new byte[1024];
                while((len = inputStream.read(b)) != -1){

                    count += len;
//                    Log.i("wjx", "onResponse: "+count);
                    fileOutputStream.write(b,0,len);

                    int cc = (int) ((count / l) * 100);
                    progress.setProgress(cc);


                }


            }
        });

4.上传文件 使用POST请求

代码演示
OkHttpClient client = new OkHttpClient.Builder()
                .callTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .build();
        MultipartBody file = new MultipartBody.Builder()
                .setType(MultipartBody.FORM).addFormDataPart("file", "wangjiaxu.mp4", 
                        RequestBody.create(MediaType.parse("Movies/mp4"), new File("/sdcard/Movies/aa.mp4")))
                .build();
        final Request request = new Request.Builder()
                .post(file).url("http://169.254.113.244/hfs/")
                .build();
        Call call = client
                .newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("wjx", "onFailure: 没传上去");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i("wjx", "onResponse: "+response.body().string());
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值