retrofit注解
@Multipart
@POST("requesturl")
Observable<ResponseBody> upload(@PartMap Map<String, RequestBody> map);
在和后台对接接口的时候,是表单类型 传入参数和图片。 其中一个参数是json格式。当时我一直在用ftpMap.toString 作为value,结果就是一直报错 500 Internal Server Error “Message”:"此资源不支持请求实体的媒体类型。然后从网上这篇博客找到下一篇博客。整整弄了一个下午,简直令人火大。好在最后还是解决了 所以有问题一直要和同事仔细对接一下,不然就是大海捞针。吐血。
HashMap<String, Object> ftpMap = new HashMap<>();
ftpMap.put("xxx", xxx);
File fileSign = new File(localPath);
if (fileSign.exists()) {//获取文件,存在就上传
Map<String, RequestBody> map = new HashMap<>();
map.put("ftpMap", RequestBody.create(MediaType.parse("text/plain"), new Gson().toJson(ftpMap).toString()));
map.put("xxx", RequestBody.create(MediaType.parse("text/plain"), "123123123"));
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), fileSign);
map.put("file\";filename=\"" + fileSign.getName(), requestBody);
RetrofitFactory.getInstance().getRetrofit()
.create(Interface.class)
.upload(map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody s) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}