implementation 'com.squareup.okhttp3:okhttp:3.11.0'
一、GET请求
注:回调的Callback在子线程执行,不能更新UI
private void doGet() { //1.获取OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2.获取Request对象 Request request = new Request.Builder().get().url("http://www.baidu.com").build(); //3.将Request封装为Call对象 Call call = okHttpClient.newCall(request); //4.执行Call call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: " + e); } @Override public void onResponse(Call call, Response response) throws IOException { Log.e(TAG, "onResponse: " + response.body().string()); } }); }
将访问百度的服务器,服务器返回的内容体便是百度首页的html
百度首页html.png
二、POST请求
private void doPost() { //1.获取OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2.构造RequestBody FormBody body = new FormBody.Builder().add("id", "26").build(); Request request = new Request.Builder().url(Cons.BASE_URL+"swords/postFind").post(body).build(); //3.将Request封装为Call对象 Call call = okHttpClient.newCall(request); //4.执行Call call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: " + e); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Log.e(TAG, "onResponse: " + result); runOnUiThread(() -> ToastUtil.showAtOnce(MainActivity.this, result)); } }); }
post请求.png
三、POST请求的请求体中添加文本
private void doPostStr() { //1.获取OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2.构造RequestBody RequestBody body = RequestBody.create(MediaType.parse("text/plain;chaset=utf-8"), "轻轻的我走了,\n" + "正如我轻轻的来;\n" + "我轻轻的招手,\n" + "作别西天的云彩。\n" + "\n" + "那河畔的金柳,\n" + "是夕阳中的新娘;\n" + "波光里的艳影,\n" + "在我的心头荡漾。\n); Request request = new Request.Builder().url(Cons.BASE_URL+"PostString").post(body).build(); //3.将Request封装为Call对象 Call call = okHttpClient.newCall(request); //4.执行Call call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: " + e); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Log.e(TAG, "onResponse: " + result); runOnUiThread(() -> ToastUtil.showAtOnce(MainActivity.this, result)); } }); }
post请求body中加入文本.png
四、通过流传输文件
private void doPostFile() { File file = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera/iv_500x400.png"); //1.获取OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2.构造Request--任意二进制流:application/octet-stream Request request = new Request.Builder() .url(Cons.BASE_URL + "PostFile") .post(RequestBody.create(MediaType.parse("application/octet-stream"), file)).build(); //3.将Request封装为Call对象 Call call = okHttpClient.newCall(request); //4.执行Call call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: " + e); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Log.e(TAG, "onResponse: " + result); runOnUiThread(() -> ToastUtil.showAtOnce(MainActivity.this, result)); } }); }
上传文件.png
五、通过模拟表单上传文件:MultipartBody
/** * 模拟表单上传文件:通过MultipartBody */private void doUpload() { File file = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera/iv_500x400.png"); RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file); //1.获取OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2.获取Request对象 RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", "test.jpg", fileBody) .build(); Request request = new Request.Builder() .url(Cons.BASE_URL + "upload") .post(requestBody).build(); //3.将Request封装为Call对象 Call call = okHttpClient.newCall(request); //4.执行Call call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: " + e); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Log.e(TAG, "onResponse: " + result); runOnUiThread(() -> ToastUtil.showAtOnce(MainActivity.this, result)); } }); }
六、下载文件:
private void doDownload() { //1.获取OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2.获取Request对象 Request request = new Request.Builder().get().url(Cons.BASE_URL + "imgs/test.jpg").build(); //3.将Request封装为Call对象 Call call = okHttpClient.newCall(request); //4.执行Call call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: " + e); } @Override public void onResponse(Call call, Response response) throws IOException { File file = new File(Environment.getExternalStorageDirectory(), "download.jpg"); InputStream is = response.body().byteStream(); FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[102]; int len = 0; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); is.close(); } }); }
下载本地.png
七、在安卓端显示图片:
有了流,一切都好办:
response.body().byteStream()
关于Bitmap的预处理等操,这里就不多说了,有兴趣的可见1-MI-Android多媒体之Bitmap:
private void showImg() { //1.获取OkHttpClient对象 OkHttpClient okHttpClient = new OkHttpClient(); //2.获取Request对象 Request request = new Request.Builder().get().url(Cons.BASE_URL + "imgs/test.jpg").build(); //3.将Request封装为Call对象 Call call = okHttpClient.newCall(request); //4.执行Call call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, "onFailure: " + e); } @Override public void onResponse(Call call, Response response) throws IOException { InputStream is = response.body().byteStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); runOnUiThread(()->{ mIdIvShow.setImageBitmap(bitmap); }); } }); }
安卓显示.png
作者:张风捷特烈
链接:https://www.jianshu.com/p/7c286f65f7d1
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦