【发布时间】: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