retrofit2 多图上传遇到的问题
2016-11-12 本文已影响163人
Michaelhanji
今天在公司 做关于图片上传的时候, 用的最新的retrofit 去进行 文字和 多图的上传, 服务端返回的数据显示 是token 过期, 刚看时还以为是 服务端的 问题, 通过 后期的 fidder 抓取数据, 看网络请求, 发现 主要的问题, 出在 的 Retrofi2 在进行 图片上传的时候 要注意
Call<String> uploadImage(@Part("description") String description, @PartMap Map<String, RequestBody> imgs1)
Call<String> uploadImage(@Part("content")RequestBody content,@Part("token") RequestBody token ,@PartMapMap<String, RequestBody> imgs1);
//关于String 类型的, 要采用 RequestBody 去进行注解
附上 关于多图片上传的代码
public class FileUploadManager {
private static final String ENDPOINT = "http://h5.7676.com/";
private static String TAG = FileUploadManager.class.getSimpleName();
public interface FileUploadService {
/** * 上传一张图片 *
@param
* @param imgs
* @return */
@Multipart
// @POST("/upload")
@POST("mapiindex.php?m=hi&c=index&a=publish")
Call<String> uploadImage(@Part("fileName") String content, @Part("file\"; filename=\"image.png\"") RequestBody imgs);
RequestBody contentbody= RequestBody.create(MediaType.parse("text/plain"), content); RequestBody tokenbody = RequestBody.create(MediaType.parse("text/plain"), QyClient.curUser.getToken()); Call<String> stringCall = apiManager.uploadImage(contentbody,tokenbody ,photos ); stringCall.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { Log.d(TAG, "onResponse() called with: " + "call = [" + call + "], response = [" + response + "]"); Logger.e("onResponse() called with: \" + \"call = [\" + call + \"],","response = [" + response + "]"); String body = response.body(); int code = response.code(); Logger.e("post image and content ---->"+body+"code--->"+code); } @Override public void onFailure(Call<String> call, Throwable t) { Logger.e("eeeeeeeeeeeeeeeeeeeeeeee","onFailure() called with: " + "call = [" + call + "], t = [" + t + "]"); Logger.e("call",t.toString()+call.toString()); } });}
}