如何使用Raturofit(Java)发送Jonobject Post请求
的发布请求
图像和其他各个字段
@Multipart
@POST("operator/upload")
Call<JsonObject>upload(@Header ("Authorization") String t,
@Part MultipartBody.Part img,
@Part("fullName") RequestBody name,
@Part("fatherName") RequestBody fathername,
@Part("aadharNumber") RequestBody aadharnumber,
@Part("dob") RequestBody dob,
@Part("registeredAddress") RequestBody address,
@Part("gender") RequestBody gender,
@Part("plantId") RequestBody plant,
@Part MultipartBody.Part data,
@Part("authorized") Boolean auth,
@Part("verified") Boolean ver) ;
一个
public void userUpload(String p, MultipartBody.Part image, RequestBody name, RequestBody fathername, RequestBody aadharnumber, RequestBody dob, RequestBody address, RequestBody gender, RequestBody plant, MultipartBody.Part cap, Boolean aut, Boolean ver, Response_listner response_listner) {
Call<JsonObject> call = api_interface.upload(p, image, name, fathername, aadharnumber, dob, address, gender, plant,cap,aut,ver);
serveCall(call, response_listner);
}
我想发送一个包含jsonobject和 方法是在这里
private void serveCall(Call<JsonObject> call, Response_listner responce_listner) {
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
try {
responce_listner.onResponseSuccess(response);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
try {
responce_listner.onResponseFailure(t);
Log.d("Network", t.getMessage());
Log.d("Network Cause", t.getCause().getMessage());
Log.d("NetworkStack", t.getStackTrace().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
,我在其中进行了API调用的主要代码
HashMap<String, String> num2 = new HashMap<>();
num2.put("client_id",String.valueOf(CLIENT_ID));
num2.put("full_name",String.valueOf(FULL_NAME));
num2.put("aadhaar_number",String.valueOf(AADHAR_NUMBER));
num2.put("dob",String.valueOf(DOB));
num2.put("gender",String.valueOf(GENDER));
num2.put("face_status",String.valueOf(FACE_STATUS));
num2.put("face_score",String.valueOf(FACE_SCORE));
num2.put("zip",String.valueOf(ZIP));
num2.put("profile_image",String.valueOf(PROFILE_IMAGE));
num2.put("has_image",String.valueOf(HAS_IMAGE));
num2.put("mobile_hash",String.valueOf(MOBILE_HASH));
num2.put("raw_xml",String.valueOf(RAW_XML));
num2.put("zip_data",String.valueOf(ZIP_DATA));
num2.put("care_of",String.valueOf(CARE_OF));
num2.put("share_code",String.valueOf(SHARE_CODE));
num2.put("mobile_verified",String.valueOf(MOBILE_VERIFIED));
num2.put("reference_id",String.valueOf(REFERENCE_ID));
MultipartBody.Part delta = MultipartBody.Part.createFormData("aadharDetails", num2.toString());
RetrofitClient.getInstance_new().userUpload(TokenData, image, name, fathername, aadharnumber, dobb, addresss, genderr, plant,delta, auth, ver, new Response_listner() {
@Override
public void onResponseSuccess(Response baseResponse) throws IOException, JSONException {
if (baseResponse.isSuccessful()) {
Log.e("Response",baseResponse.body().toString());
JSONObject jsonObject = new JSONObject(baseResponse.body().toString());
prog_close();
Intent intent = new Intent(RegisterOP.this, Done.class);
startActivity(intent);
finish();
}
// }
} else {
Toast.makeText(RegisterOP.this, baseResponse.message(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onResponseFailure(Throwable throwable) {
Toast.makeText(RegisterOP.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
});
,因此我发送的所有数据都不错,除了 delta 它是请求主体以某种方式发送字符串而不是hashmap
您的帮助!呢
I want to send a post request which contains jsonObject and an image and various other fields and I used MultiPart for image and have successfully uploaded the image but the jsonObject is uploaded in string format
Here is my Api Interface
@Multipart
@POST("operator/upload")
Call<JsonObject>upload(@Header ("Authorization") String t,
@Part MultipartBody.Part img,
@Part("fullName") RequestBody name,
@Part("fatherName") RequestBody fathername,
@Part("aadharNumber") RequestBody aadharnumber,
@Part("dob") RequestBody dob,
@Part("registeredAddress") RequestBody address,
@Part("gender") RequestBody gender,
@Part("plantId") RequestBody plant,
@Part MultipartBody.Part data,
@Part("authorized") Boolean auth,
@Part("verified") Boolean ver) ;
Here is the Retrofit service function
public void userUpload(String p, MultipartBody.Part image, RequestBody name, RequestBody fathername, RequestBody aadharnumber, RequestBody dob, RequestBody address, RequestBody gender, RequestBody plant, MultipartBody.Part cap, Boolean aut, Boolean ver, Response_listner response_listner) {
Call<JsonObject> call = api_interface.upload(p, image, name, fathername, aadharnumber, dob, address, gender, plant,cap,aut,ver);
serveCall(call, response_listner);
}
The ServeCall Method is here
private void serveCall(Call<JsonObject> call, Response_listner responce_listner) {
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
try {
responce_listner.onResponseSuccess(response);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
try {
responce_listner.onResponseFailure(t);
Log.d("Network", t.getMessage());
Log.d("Network Cause", t.getCause().getMessage());
Log.d("NetworkStack", t.getStackTrace().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
And My Main Code where I Made an Api Call
HashMap<String, String> num2 = new HashMap<>();
num2.put("client_id",String.valueOf(CLIENT_ID));
num2.put("full_name",String.valueOf(FULL_NAME));
num2.put("aadhaar_number",String.valueOf(AADHAR_NUMBER));
num2.put("dob",String.valueOf(DOB));
num2.put("gender",String.valueOf(GENDER));
num2.put("face_status",String.valueOf(FACE_STATUS));
num2.put("face_score",String.valueOf(FACE_SCORE));
num2.put("zip",String.valueOf(ZIP));
num2.put("profile_image",String.valueOf(PROFILE_IMAGE));
num2.put("has_image",String.valueOf(HAS_IMAGE));
num2.put("mobile_hash",String.valueOf(MOBILE_HASH));
num2.put("raw_xml",String.valueOf(RAW_XML));
num2.put("zip_data",String.valueOf(ZIP_DATA));
num2.put("care_of",String.valueOf(CARE_OF));
num2.put("share_code",String.valueOf(SHARE_CODE));
num2.put("mobile_verified",String.valueOf(MOBILE_VERIFIED));
num2.put("reference_id",String.valueOf(REFERENCE_ID));
MultipartBody.Part delta = MultipartBody.Part.createFormData("aadharDetails", num2.toString());
RetrofitClient.getInstance_new().userUpload(TokenData, image, name, fathername, aadharnumber, dobb, addresss, genderr, plant,delta, auth, ver, new Response_listner() {
@Override
public void onResponseSuccess(Response baseResponse) throws IOException, JSONException {
if (baseResponse.isSuccessful()) {
Log.e("Response",baseResponse.body().toString());
JSONObject jsonObject = new JSONObject(baseResponse.body().toString());
prog_close();
Intent intent = new Intent(RegisterOP.this, Done.class);
startActivity(intent);
finish();
}
// }
} else {
Toast.makeText(RegisterOP.this, baseResponse.message(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onResponseFailure(Throwable throwable) {
Toast.makeText(RegisterOP.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
});
So the data that I'm sending all is good except delta which is request body somehow it sends string rather than HashMap
Your Help Is Greatly Appreciated !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论