Android基于OkHttp实现下载和上传图片
程序员文章站
2023-11-18 17:10:46
本文实例为大家分享了okhttp实现下载图片和上传图片的具体代码,供大家参考,具体内容如下
mainactivity.java
public class mai...
本文实例为大家分享了okhttp实现下载图片和上传图片的具体代码,供大家参考,具体内容如下
mainactivity.java
public class mainactivity extends appcompatactivity { private string path = "https://10.url.cn/eth/ajnvdqhzllaxibwnroxxszixa76ichutwmccopa45xjiapnemzsib7ey4wuxf6xdml2fmzevysf86iaw/"; private static final int success = 993; private static final int fall = 814; handler handler = new handler() { @override public void handlemessage(message msg) { switch (msg.what) { //加载网络成功,进行ui的更新,处理得到的图片资源 case success: //通过message,拿到字节数组 byte[] picture = (byte[]) msg.obj; //使用bitmapfactory工厂,把字节数组转换为bitmap bitmap bitmap = bitmapfactory.decodebytearray(picture, 0, picture.length); //通过imageview,设置图片 mimageview_okhttp.setimagebitmap(bitmap); break; //当加载网络失败,执行的逻辑代码 case fall: toast.maketext(mainactivity.this, "网络异常", toast.length_short).show(); break; default: break; } } }; private imageview mimageview_okhttp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //初始化控件 initview(); } private void initview() { mimageview_okhttp = (imageview) findviewbyid(r.id.imageview_okhttp); } /** * 根据点击事件获取络上的图片资源,使用的是okhttp框架 * * @param view */ public void picture_okhttp_bt(view view) { //1. 创建okhttpclient对象 okhttpclient okhttpclient = new okhttpclient(); //2.建立request对象,设置参数,请求方式如果是get,就不用设置,默认使用的就是get request request = new request.builder() .url(path)//设置请求网址 .build();//建立request对象 //3.创建一个call对象,参数是request对象,发送请求 call call = okhttpclient.newcall(request); //4.异步请求,请求加入调度 call.enqueue(new callback() { @override//请求失败回调 public void onfailure(call call, ioexception e) { handler.sendemptymessage(fall); } @override//请求成功回调 public void onresponse(call call, response response) throws ioexception { //得到从网上获取资源,转换成我们想要的类型 byte[] picture_bt = response.body().bytes(); //通过handler更新ui message message = handler.obtainmessage(); message.obj = picture_bt; message.what = success; handler.sendmessage(message); } }); } //当按钮点击时,执行使用okhttp上传图片到服务器(http://blog.csdn.net/tangxl2008008/article/details/51777355) //注意:有时候上传图片失败,是服务器规定还要上传一个key,如果开发中关于网络这一块出现问题,就多和面试官沟通沟通 public void uploading(view view) { //图片上传接口地址 string url = "https://www.718shop.com/sell/sell.m.picture.upload.do"; //创建上传文件对象 file file = new file(environment.getexternalstoragedirectory(), "big.jpg"); //创建requestbody封装参数 requestbody filebody = requestbody.create(mediatype.parse("application/octet-stream"), file); //创建multipartbody,给requestbody进行设置 multipartbody multipartbody = new multipartbody.builder() .settype(multipartbody.form) .addformdatapart("image", "big.jpg", filebody) .build(); //创建request request request = new request.builder() .url(url) .post(multipartbody) .build(); //创建okhttp对象 okhttpclient okhttpclient = new okhttpclient.builder() .connecttimeout(10, timeunit.seconds) .readtimeout(10, timeunit.seconds) .writetimeout(10, timeunit.seconds) .build(); //上传完图片,得到服务器反馈数据 okhttpclient.newcall(request).enqueue(new callback() { @override public void onfailure(call call, ioexception e) { log.e("ff", "uploadmultifile() e=" + e); } @override public void onresponse(call call, response response) throws ioexception { log.i("ff", "uploadmultifile() response=" + response.body().string()); } }); } }
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.sn.picture_okhttp.mainactivity"> <button android:id="@+id/button" android:onclick="picture_okhttp_bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下载图片" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="59dp"/> <button android:text="上传图片" android:onclick="uploading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:layout_alignparenttop="true" android:layout_alignleft="@+id/button" android:layout_alignstart="@+id/button"/> <imageview android:id="@+id/imageview_okhttp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true"/> </relativelayout>
build.gradle //依赖
implementation 'com.squareup.okhttp3:okhttp:3.4.2'
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: ASP.NET微信公众号查看粉丝信息接口
下一篇: Android购物分类效果实现
推荐阅读