文:http://blog.csdn.net/sk719887916/article/details/51755427
Tamic 简书&csdn同步 获取技术文章请关注底部微信公众号!
通过前两篇姿势的入门
本文出自:http://blog.csdn.net/sk719887916/article/details/51755427 码小白
通过对Retrofit2.0的前两篇的基础入门和案例实践,掌握了怎么样使用Retrofit访问网络,加入自定义header,包括加入SSL证书,基本的调试基础,coolkie同步,但很多需求需要文件的上传,今天主题就来分享怎么用Retrofit2.0 上传文件和图片。
使用 Retrofit1.x上传文件
大家都知道在2.0以前版本上传图片的姿势
public interface ApiManager {
@Multipart
@POST("/user/addCarInfo")
void addCarInfo(@QueryMap Map<String, Object> options, @Part("file") TypedFile file, Callback<JsonElement> response);
}
使用 Retrofit 2.X 上传
Retrofit 2上传文件
使用2,0,我们发现TypedFile类型被私有化了 ,无法继续使用1.9的传方式无法再上层调用了,可以MultipartBody.Part代替
public interface FileUploadService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(@Part("description") RequestBody description,
@Part MultipartBody.Part file);
}
具体用法。
// 创建 RequestBody,用于封装 请求RequestBody
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send a