如何使用Raturofit(Java)发送Jonobject Post请求

发布于 2025-02-08 04:19:38 字数 5631 浏览 2 评论 0原文

的发布请求

图像和其他各个字段

 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文