【问题标题】:While calling a post request through Retrofit getting 400 bad request error通过 Retrofit 调用发布请求时出现 400 bad request 错误
【发布时间】:2021-08-29 12:14:33
【问题描述】:

通过 Postman API 可以工作,但相同的 API 不能与 Retrofit 一起工作 我收到 400 错误请求并进行改造

返回真或假的端点 @POST("/emregax/api/allscript/saveappointment") 调用 saveAppointment(@Header("x-auth-token") String header1, @Body String yy);

    and this is my method from where i am calling this above API
    private void saveAppoint(String token, Saveappointment mSaveappointment){
    APIInterface service = APIClient.getRetrofitInstance().create(APIInterface.class);
    JSONObject paramObject = new JSONObject();
    paramObject.put("ehrAppointmentId", "");
    paramObject.put("ehrPatientId", mSaveappointment.getEhrPatientId());
    paramObject.put("appointmentType", mSaveappointment.getAppointmentType());
    paramObject.put("ehrProviderAbbrev", mSaveappointment.getEhrProviderAbbrev());
    paramObject.put("ehrProviderId", mSaveappointment.getEhrProviderId());
    paramObject.put("reason", mSaveappointment.getReason());
    paramObject.put("startDateTime", mSaveappointment.getStartDateTime());

    Call<Boolean> call = service.saveAppointment(token, paramObject.toString());
    call.enqueue(new Callback<Boolean>() {
        @Override
        public void onResponse(Call<Boolean> call, Response<Boolean> response) {
            progressDialog.dismiss();
            //boolean value=response.body();
            if (!response.isSuccessful()) {

                Toast.makeText(mcontext, "" + response.code() + " " + response.body(), Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFailure(Call<Boolean> call, Throwable t) {
          Toast.makeText(mcontext, "" + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
   }

【问题讨论】:

    标签: android retrofit2


    【解决方案1】:

    400 Bad Request 错误意味着客户端发出的请求不正确或损坏,服务器无法理解(解析)它。 400 Bad Request 错误的一些原因:

    • 网址无效。
    • 无效/缺少标头。

    也许响应错误正文包含一些详细信息?

    【讨论】:

    • 我尝试了很多方法都没有成功。然后尝试了凌空抽射,效果很好。谢谢
    猜你喜欢
    • 2014-08-22
    • 1970-01-01
    • 2019-06-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多