【发布时间】:2017-07-18 09:41:55
【问题描述】:
我正在尝试使用改造将多个图像发送到服务器 我正在做的是发送 RequestBody 的地图,这是我的代码
@Multipart
@POST("imageuload")
Call<ResponseBody> postImage(@PartMap Map<String, RequestBody> files );
在我的活动中
Map<String, RequestBody> filestosend = new HashMap<>();
for (int pos = 0; pos < files.size(); pos++) {
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), files.get(pos));
filestosend.put("photo_" + String.valueOf(pos + 1), requestBody);
}
Call<ResponseBody> call = apiSerice.postImage(filestosend);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
Toast.makeText(getBaseContext(),response.body().string(),Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}else {
try {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage(response.errorBody().string());
alert.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
当我想测试我得到什么时,它会从服务器返回一个空的响应,而我的响应中什么也没得到..
echo file_get_contents('php://input');
我什至使用单个请求正文进行了测试
RequestBody test = RequestBody.create(MediaType.parse("text/plain"), "test");
Call<ResponseBody> call = apiSerice.postImage(test);
但我在回复中仍然得到一个空的回复 我将不胜感激任何帮助或评论
【问题讨论】: