如何使用改造 2.0 发送包含图像数组和其他详细信息的 jsonObject?



当我在POSTMAN中尝试该请求时,它工作正常。 但是,当我尝试使用改造 2.0 做同样的事情时;我总是收到错误 500,我无法找出我做错了什么!

下面的代码是使用改造的调用:

@Headers({"Content-Type: application/json"})
@POST("relatorio/{token}")
Call<JsonObject> uploadJson(@Path("token") String token, @Body JsonObject auditoria);
Call<JsonObject> call = uploadJson(details.get(KEY_TOKEN), json);

而且,在这里我有在调用中传递的参数的构造:

JsonObject json = new JsonObject();
JsonArray steps = new JsonArray();
steps.add(new JsonParser().parse(data.getStringExtra("json")).getAsJsonObject());
json.add("steps", steps);
json.addProperty("title", basic.getString("title"));
json.addProperty("description", basic.getString("description"));
json.addProperty("autor", basic.getString("autor"));
json.addProperty("responsavel", details.get(KEY_USERNAME));
json.add("imagens", prepare());
private JsonArray prepare(){
Set<String> keys = JsonFormFragmentPresenter.imagesList.keySet();
JsonArray imagens = new JsonArray();
for(String k : keys){
for(String path : JsonFormFragmentPresenter.imagesList.get(k)){
if(!path.equals(null) && !path.equals("")){
imagens.add(path);
}
}
}
JsonFormFragmentPresenter.imagesList.clear();
return imagens;
}

谢谢

您需要使用多部分格式

这是下面的康德示例

@Multipart
@POST("sync/contact/image")
Call<Response> ImageUpload(@Part MultipartBody.Part file);
@Multipart
@POST("sync/image")
Call<ResponseBody> MultiImageUpload(@PartMap() Map<String, RequestBody> mapFileAndName);

public static HashMap<String, RequestBody> GetAllImage(Context context) {
File files = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/.ELMEX");
File[] filesArray = files.listFiles();
List<File> listOfNames = Arrays.asList(filesArray);
HashMap<String, RequestBody> map = new HashMap<>(listOfNames.size());
RequestBody file = null;
for (int i = 0, size = listOfNames.size(); i < size; i++) {
file = RequestBody.create(MediaType.parse("multipart/form-data"), listOfNames.get(i));
map.put("file"; filename="" + listOfNames.get(i).getName() + ".jpg", file);
file = null;
}
return  map;
}

HashMap<String, RequestBody> map = UtilImage.GetAllImage(context);
Call<ResponseBody> call = Retro.getRetroWS().MultiImageUpload(map);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, "onResponse: ");
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(TAG, "onFailure: ");
}
});

最新更新