Retrofit实现图文上传至服务器
程序员文章站
2023-11-15 20:40:58
前言:现在大多数的项目中都涉及图片+文字上传了,下面请详见实现原理:
开发环境:androidstudio
1.引入依赖:
compile 'com.squareup...
前言:现在大多数的项目中都涉及图片+文字上传了,下面请详见实现原理:
开发环境:androidstudio
1.引入依赖:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
2.网络权限:
<uses-permission android:name="android.permission.internet" />
3.创建上传对象okhttpclient :
private static final okhttpclient client = new okhttpclient.builder() .addinterceptor(new interceptor() { @override public response intercept(chain chain) throws ioexception { request request = chain .request() .newbuilder() .build(); return chain.proceed(request); } }) .readtimeout(10, timeunit.seconds)//设置读取超时时间 .writetimeout(10, timeunit.seconds)//设置写的超时时间 .connecttimeout(15, timeunit.seconds)//设置连接超时时间 .build();
4.上传图片的公有方法:
private synchronized final static void uploadimgandparameter(map<string, object> map, string url, final uidatalistener listener) { // mimgurls为存放图片的url集合 multipartbody.builder builder = new multipartbody.builder().settype(multipartbody.form); if (null != map) { for (map.entry<string, object> entry : map.entryset()) { if (entry.getvalue() != null) { if (entry.getvalue() instanceof file) { file f = (file) entry.getvalue(); builder.addformdatapart(entry.getkey(), f.getname(), requestbody.create(media_type_png, f)); } else { builder.addformdatapart(entry.getkey(), entry.getvalue().tostring()); } } } } //创建requestbody requestbody body = builder.build(); // multipartbody requestbody = builder.build(); //构建request请求 final request request = new request.builder() .url(url)//地址 .post(body)//添加请求体 // .post(requestbody)//添加请求体 .build(); client.newcall(request).enqueue(new okhttp3.callback() { @override public void onresponse(call call, final response response) throws ioexception { if (response.issuccessful()) {//判断是否成功 final string data = response.body().string();//string()仅可调用一次。否则报illegalstateexception: closed异常 log.i("file1", "上传照片成功-->" + data); onsuccess(listener, data); call.cancel();//上传成功取消请求释放内存 } } @override public void onfailure(call call, final ioexception e) { log.i("file2", "上传失败-->" + e.getmessage()); string msg = e.getmessage(); if (msg == null || msg.equals("timeout")) { onerror(listener, "网络不稳定请求超时!"); } else { onerror(listener, e.getmessage()); } call.cancel();//上传失败取消请求释放内存 } }); }
//注意:添加手机图片,别忘了添加sd卡权限
5.全部代码:
public class httputil { private static final handler handler = new handler(looper.getmainlooper()); private static final mediatype media_type_png = mediatype.parse("image/*"); private static final okhttpclient client = new okhttpclient.builder() .addinterceptor(new interceptor() { @override public response intercept(chain chain) throws ioexception { request request = chain .request() .newbuilder() .build(); return chain.proceed(request); } }) .readtimeout(10, timeunit.seconds)//设置读取超时时间 .writetimeout(10, timeunit.seconds)//设置写的超时时间 .connecttimeout(15, timeunit.seconds)//设置连接超时时间 .build(); /** * 实例--》添加商品 */ public static void addcoupon( int shopperid,string shoppername, file file, final uidatalistener listener) { string url = "shopappajx/shopappcouponaction_savecoupon.htm"; map<string, object> map = new hashmap<>(); map.put("shopperid", shopperid); map.put("shoppername", shoppername); map.put("couponimage", file);//商品图片 uploadimgandparameter(map, url, listener); } //上传图片共有方法 private synchronized final static void uploadimgandparameter(map<string, object> map, string url, final uidatalistener listener) { // mimgurls为存放图片的url集合 multipartbody.builder builder = new multipartbody.builder().settype(multipartbody.form); if (null != map) { for (map.entry<string, object> entry : map.entryset()) { if (entry.getvalue() != null) { if (entry.getvalue() instanceof file) { file f = (file) entry.getvalue(); builder.addformdatapart(entry.getkey(), f.getname(), requestbody.create(media_type_png, f)); } else { builder.addformdatapart(entry.getkey(), entry.getvalue().tostring()); } } } } //创建requestbody requestbody body = builder.build(); // multipartbody requestbody = builder.build(); //构建request请求 final request request = new request.builder() .url(url)//地址 .post(body)//添加请求体 // .post(requestbody)//添加请求体 .build(); client.newcall(request).enqueue(new okhttp3.callback() { @override public void onresponse(call call, final response response) throws ioexception { if (response.issuccessful()) {//判断是否成功 final string data = response.body().string();//string()仅可调用一次。否则报illegalstateexception: closed异常 log.i("file1", "上传照片成功-->" + data); onsuccess(listener, data); call.cancel();//上传成功取消请求释放内存 } } @override public void onfailure(call call, final ioexception e) { log.i("file2", "上传失败-->" + e.getmessage()); string msg = e.getmessage(); if (msg == null || msg.equals("timeout")) { onerror(listener, "网络不稳定请求超时!"); } else { onerror(listener, e.getmessage()); } call.cancel();//上传失败取消请求释放内存 } }); } private final static void onsuccess(final uidatalistener listener, final string data) { handler.post(new runnable() { public void run() { // 需要在主线程的操作。 listener.onsuccess(data); } }); } private final static void onerror(final uidatalistener listener, final string msg) { if (null != listener) { handler.post(new runnable() { public void run() { // 需要在主线程的操作。 listener.onfailure(msg); } }); } } public interface uidatalistener { //网络请求成功 void onsuccess(string data); //网络请求失败 void onfailure(string errormassage); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: 开通迅雷存迅雷存钱罐的方法介绍