OkHttp3与旧版本OkHttp的区别分析与下载图片

okhttp3与旧版本okhttp的区别分析
http://www.w2bc.com/article/132044


okhttp3
http://blog.csdn.net/itachi85/article/details/51190687


//上传图片
http://blog.csdn.net/tangxl2008008/article/details/51777355


okhttp3.0的依赖


compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okio:okio:1.5.0'


   //下载图片
    private void uploadPic() {
        //图片上传的接口地址
        String url = "http://169.254.249.24:8080/08web/FileUploadServlet";


        //得到图片
        File file = new File(Environment.getExternalStorageDirectory(), "/Pictures/Screenshots/a.jpg");
        //创建RequestBody来封装参数
        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
            //创建RequestBody 进行设置
    okhttp3.RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "a.jpg", fileBody).build();
        okhttp3.Request request = new okhttp3.Request.Builder().url(url).post(requestBody).build();
        //创建OkHttp3 通过创建者设计模式
        okhttp3.OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder();
        okhttp3.OkHttpClient okHttpClient = builder.connectTimeout(2000, TimeUnit.MILLISECONDS).writeTimeout(2000, TimeUnit.MILLISECONDS).build();
        okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                Log.i("xxx", "上传失败");


            }


            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                Log.i("xxx", "上传成功");
            }
        });


    }

你可能感兴趣的:(OkHttp3与旧版本OkHttp的区别分析与下载图片)