Retrofit实现图文上传至服务器

前言:现在大多数的项目中都涉及图片+文字上传了,下面请详见实现原理:

开发环境: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);
}
}

Retrofit实现图文上传至服务器的更多相关文章

  1. 【Android实战】----基于Retrofit实现多图片/文件、图文上传

    本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...

  2. DedeCMS使用方法----如何将网站上传到服务器

    我们如果在本地已经把网站做好了,上传到服务器上去的正确姿势是什么样的呢?简单的很~跟着我的步调来~ 方法一(推荐此方法): 1.把你本地所有的文件压缩,上传至服务器上的根目录,再解压. 2.把本地的数 ...

  3. 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)

    使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)   原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...

  4. Phpcms V9网站从本地上传到服务器需要修改的地方

    网站在本地做好后要迁移到服务器上:网站在发展的过程中,很可能多次的修改域名.那么在Phpcms V9中我们要怎么进行设置呢 请进行以下步骤的修改: Phpcms V9网站上传到服务器具体方法如下:  ...

  5. java文件上传到服务器

    最近项目中使用到了文件从本地到服务器的功能.其实是为了解决目前浏览器不支持获取本地文件全路径.不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据. 在前台界 ...

  6. Maven配置jar(war)包自动打包上传Maven服务器的配置

    Maven配置jar(war)包自动打包上传Maven服务器的配置 创建jar(war)包工程 创建一个maven工程 在工程中穿件一个测试类 配置pom.xml <distributionMa ...

  7. HTML5 文件域+FileReader 分段读取文件并上传到服务器(六)

    说明:使用Ajax方式上传,文件不能过大,最好小于三四百兆,因为过多的连续Ajax请求会使后台崩溃,获取InputStream中数据会为空,尤其在Google浏览器测试过程中. 1.简单分段读取文件为 ...

  8. HTML5 文件域+FileReader 读取文件并上传到服务器(三)

    一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...

  9. HTML5 Canvas前台压缩图片并上传到服务器

    1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...

随机推荐

  1. Angular material mat-icon 资源参考_File

    ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...

  2. Salesforce LINKS

    Salesforce的二次开发平台的多租户架构 http://blog.talkingdata.net/?p=4807 Salesforce 简介 https://www.cnblogs.com/ch ...

  3. R 拆分EXCEL成多个文件

    setwd("C:/Rworkfile") install.packages("readxl") library(readxl) www<-read_ex ...

  4. D3.js绘制平行坐标图

    参照:https://syntagmatic.github.io/parallel-coordinates/ 和 https://github.com/syntagmatic/parallel-coo ...

  5. 小y的质数

    题目链接:https://ac.nowcoder.com/acm/contest/634/C 链接:https://ac.nowcoder.com/acm/contest/634/C来源:牛客网 题目 ...

  6. reduce的用法

    在不增加变量的情况下,统计数组中各元素出现的次数. ```jsfunction countItem (arr) { // 写入你的代码}countItem(['a', 'b', 'a', 'c', ' ...

  7. [JAVA]流控及超流控后的延迟处理

    流控检查(每半秒累计,因此最小留空阀值只能做到每秒2条): import java.text.SimpleDateFormat; import java.util.Date; import java. ...

  8. KOA 与 CO 实现浅析

    KOA 与 CO 的实现都非常的短小精悍,只需要花费很短的时间就可以将源代码通读一遍.以下是一些浅要的分析. 如何用 node 实现一个 web 服务器 既然 KOA 实现了 web 服务器,那我们就 ...

  9. 010-JedisUtils工具类模板

    redis.properties配置文件 redis.maxIdle=30 redis.minIdle=10 redis.maxTotal=100 redis.url=192.168.204.128 ...

  10. Java中子类覆盖父类方法所必须满足的条件

    因为太喜欢,所以转来,侵删! 参考自:http://www.it165.net/pro/html/201504/39284.html 一.描述 子类重写(覆盖)父类的方法必须满足的条件:1.父类中的方 ...