OKHttp上传下载

okhttp之上传

//点击事件
  public  void  sctp(View view)
    {

//创建OKHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();


        //上传的文件路劲(手机sd卡的位置)
        File file = new    File(Environment.getExternalStorageDirectory(),"img2.png");

        //创建请求体
        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"),file);


        //复杂的文件上传类
        MultipartBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file","test.png",fileBody)
                .build();


        Request request = new Request.Builder()
                .post(body)
                .url("https://www.zhaoapi.cn/file/upload")
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


                System.out.println("上传失败");

            }

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


                System.out.println("上传成功:"+response.body().string());

            }
        });



    }

okhttp下载

   //下载图片
    public void  xztp(View view)
    {

        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://msoftdl.360.cn/mobile/shouji360/360safesis/198227/360MobileSafe_7.7.3.1016.apk")
                .get()
                .build();

        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {


                System.out.println("下载失败");

            }

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

                //得到服务器所传的文件内容

                InputStream is = response.body().byteStream();


                byte[] buffer = new byte[1024];

                int len;

                FileOutputStream fos = null;
                try {

                    fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(),"360mobile.apk"));


                    while((len = is.read(buffer)) != -1){

                        fos.write(buffer,0,len);

                        fos.flush();

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fos != null){
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }


                System.out.println("下载成功");


            }
        });
 }

*要点:
1.权限

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

2.图片img2.png的路径(需要自己导入手机sd)
Tools–>Android–>Android Device Monitor(找到模拟器)
夜神模拟器sd卡路径
mnt–>shell–>emulated*

阅读更多
文章标签: okhttp
个人分类: Android
想对作者说点什么? 我来说一句

OKHTTP上传下载

ok

wittybread wittybread

2017-12-07 20:56:06

阅读数:65

OKHttp集成上传下载的进度显示

2016年05月17日 6.54MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭