【问题标题】:Retrofit2 : How to upload image with JSONObject in androidRetrofit2:如何在 android 中使用 JSONObject 上传图像
【发布时间】:2018-01-18 12:59:54
【问题描述】:

我正在尝试使用相机运行时拍摄的一张图像发布JSONObject。 如何在 android 中使用改造发布图像。 这是我的界面

 @Multipart
@POST("/upload")
Call<Response> getDetails(@Part("empsno") String  empsno,
                                @Part("time")String deliveryTime,
                                @Part("uploadFile") MultipartBody.Part part,
                                @Part("remarks")String remarks,
                                @Part("receiver")String receivedBy,
                                @Part("Address")String ipAddress
                                );

我用来上传图片和其他细节的代码

 JSONObject oJSONObject = new JSONObject();
        oJSONObject.put("empsno", strEmpsno);
        oJSONObject.put("time", strtime);
        oJSONObject.put("remarks", strRemarks);
        oJSONObject.put("receiver", strReceiver);
        oJSONObject.put("Address", straddress);
        oJSONObject.put("uploadFile", imageFolderPath + "/" + imageName);

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("uploadFile", file.getName(), requestFile);

xInterface interface = retrofit.create(xInterface.class);
Call<Response> podResponsecall = interface.getDetails(strEmpsno, strtime,
                body, strRemarks, strReceiver, straddress);


  podResponsecall.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Response<Response> response) {
                String val = response.body() + "";
                Log.e(TAG, "onResponse: " + val);
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "onFailure: " + t.getLocalizedMessage());
            }
        });

输出 - onFailure:JSON 必须以数组或对象开头。

我不知道这是对还是错。请帮助我使用 Retrofit2 beta 3 发布一些图片以及其他详细信息。

【问题讨论】:

  • 尝试在所有参数中替换RequestBody而不是String
  • 明白我的意思吗? ?
  • 以及如何在java中请求文件?像这样RequestBody xemp = RequestBody.create(MediaType.parse("multipart/form-data"), strEmpsno); MultipartBody.Part emp = MultipartBody.Part.createFormData("empsno",xemp);
  • 我编辑了我的答案检查它
  • @Amir - 现在我得到了我想要的东西。那个邮递员教程帮助了我。实际上我正在使用错误的响应 pojo 类

标签: android retrofit retrofit2


【解决方案1】:

服务类:

    @Multipart
    @POST("upload")
    Call<Void> upload(@Part("model") RequestBody model,  @Part MultipartBody.Part file); 

改造 2 部分:

    Gson gson = new GsonBuilder().setLenient().create();
    RequestBody modelBody = RequestBody.create(MediaType.parse("application/json"), gson.toJson(modelPojo));
    RequestBody reqFileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part mPart1 = MultipartBody.Part.createFormData("file","name",reqFileBody);

Java 春天:

   @PostMapping(value="/upload" ,consumes = {"multipart/form-data","application/json"})
    public ResponseEntity<Object> uploadImagebyEmail(@RequestPart("model") Object model,@RequestPart("file") MultipartFile uploadfile  ) throws IOException {
    return //codes here...   
    }   

【讨论】:

  • 是否可以只发送一个包含多部分图像和其他参数(如字符串)的 POJO?如果可以,我该怎么做?
  • 您可以将图像作为 base64 字符串发送到对象中,否则我不知道任何方式。
【解决方案2】:

替换

@Multipart
@POST("/upload")
Call<ResponseBody> getDetails(@Part("empsno") RequestBody empsno,
                                @Part("time")RequestBody deliveryTime,
                                @Part("uploadFile") MultipartBody.Part part,
                                @Part("remarks")RequestBody remarks,
                                @Part("receiver")RequestBody receivedBy,
                                @Part("Address")RequestBody ipAddress
                                );

还有在

Call<ResponseBody> podResponsecall = interface.getDetails(strEmpsno, strtime,
                body, strRemarks, strReceiver, straddress);

【讨论】:

  • RequestBody - 是 pojo 类吗?
  • 但我在这里使用 strEmpsno 是字符串格式
  • 所有我需要将其更改为 RequestBody 像 RequestBody empsno = RequestBody.create(MediaType.parse("multipart/form-data") , strEmpsno);
  • 我尝试了你建议的方式,但在 response.body() 中得到了 null
猜你喜欢
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
  • 2016-10-22
  • 2020-12-05
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多