android http3 下篇

继续学习android http3 ,这里是上一节的补充,首先说一下引用的问题,将gradle 粘贴过去,即可进行引用

compile 'com.squareup.okhttp3:okhttp:3.7.0'
接下来是总的xml文件的权限问题
android:name="android.permission.INTERNET"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


继续上一章的学习

 private void doPostString() {
        //每一次修改的时候,都需要
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=utf-8"), "this is json data or string data");
        Request.Builder builder = new Request.Builder();
        Request request = builder.post(requestBody).url(BASE_URL + "postString").build();
        execute(request);
    }
  private void doDownLoad() {
        Request.Builder builder = new Request.Builder();
        Request request = builder.get().url(BASE_URL + "files/test2.jpg").build();
        okhttp3.Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                Log.e("TAG", "Error" + e);
            }
            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                //显示到imageView上
                // showImgInImageView(response);
                //保存到本地,
                downSavetoFile(response);
            }
        });
    }

    /**
     * 保存到本地
     *
     * @param response
     * @throws IOException
     */
    private void downSavetoFile(Response response) throws IOException {
        long total = response.body().contentLength();
        long sum = 0L;
        Log.d("TAG", "downFile...");
        InputStream is = response.body().byteStream();
        int len;
        File file = new File(Environment.getExternalStorageDirectory(), "12346.jpg");
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buf = new byte[128];
        while ((len = is.read(buf)) != -1) {
            fos.write(buf, 0, len);
            sum += len;
            Log.d("TAG", "--------进度" + sum + " / " + total);
        }
        fos.flush();
        fos.close();
        is.close();
        Log.d("TAG", "down success!");
    }

    //显示图片到imageView上
    private void showImgInImageView(Response response) {
        InputStream is = response.body().byteStream();
        final Bitmap bitmap = BitmapFactory.decodeStream(is);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mDownImage.setImageBitmap(bitmap);
            }
        });
    }

    private void doPostFile() {
        File file = new File(Environment.getExternalStorageDirectory(), "test.jpg");
        if (!file.exists()) {
            Log.e("TAG", "file is not exit!");
            return;
        }

        // 可以搜索mime type 获得MediaType.parse()里面的值与对象
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        Request.Builder builder = new Request.Builder();
        Request request = builder.post(requestBody).url(BASE_URL + "postFile").build();
        execute(request);
    }


    private void doUpload() {
        File file = new File(Environment.getExternalStorageDirectory(), "test.jpg");
        if (!file.exists()) {
            Log.e("TAG", "file is not exit!");
            return;
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        MultipartBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("username", "123465")
                .addFormDataPart("passwd", "pppp")
                .addFormDataPart("mPhoto", "test2.jpg", requestBody)
                .build();
        CountingRequestBody countingRequestBody = new CountingRequestBody(multipartBody, new CountingRequestBody.Listener() {
            @Override
            public void onRequestProgress(long byteWrited, long contentLength) {
                Log.d("上传进度", byteWrited + " / " + contentLength);
            }
        });

        Request.Builder builder = new Request.Builder();
        Request request = builder.post(countingRequestBody).url(BASE_URL + "uploadImg").build();
        execute(request);
    }
记得这里的方法是这样的。可以将程序打到界面上

  runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mDownImage.setImageBitmap(bitmap);
            }
        });






你可能感兴趣的:(android)