当前位置: 首页 > 知识库问答 >
问题:

得到错误代码400错误请求在改型,但工作在邮递员

阎作人
2023-03-14

我正在开发一个Registraion API,它接受以下/request perameters的json输入

{"httpMethod":"POST",
"firstname":"Ali",
"lastname":"Patel",
"email":"alipatel05@gmail.com",
"password":"12345678",
"country":"Canada",
"state":"Quebec",
"city":"Montreal",
"type":"Parent"}

但是当我从android应用程序调用API时,它给出了错误的响应,错误代码为400,但在Postman上工作得很好。

我的API客户端

    public class APIClient {
    private static Retrofit retrofit = null;
    public static final String BASE_URL = "https://5r8ndtx9zc.execute-api.us-east-2.amazonaws.com/";

    public static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        /*Gson gson = new GsonBuilder()
                .setLenient()
                .create();*/

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        return retrofit;
    }
}
public interface APIInterface {

    @Headers({
            "Content-Type: application/json;charset=utf-8",
            "Accept: application/json;charset=utf-8",
            "Cache-Control: no-cache"
    })
    @POST("vaccinesApi")
    Call<ResponseBody> registerUser(@Body JSONObject locationPost);

}
private void registerUser() {

        HashMap<String, String> newhashMap = new HashMap<>();
        JSONObject hashMap = new JSONObject();
        try {
            hashMap.put("httpMethod","POST");
            hashMap.put("firstname",mEditFirstName.getText().toString().trim());
            hashMap.put("lastname",mEditLastName.getText().toString().trim());
            hashMap.put("email",mEditEmail.getText().toString().trim());
            hashMap.put("password",mEditPassword.getText().toString().trim());
            hashMap.put("country","Canada");
            hashMap.put("state","Quebec");
            hashMap.put("city",mSelectedCity);
            hashMap.put("type",mUserType);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Call<ResponseBody> call = apiInterface.registerUser(hashMap);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                progressDialog.hide();
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    Log.e("SignupFragment", jsonObject.toString());
                    if (response.code() == 200) {
                        Toast.makeText(RegistrationActivity.this, "Success",Toast.LENGTH_SHORT).show();
                    /*Intent intent = new Intent(RegistrationActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finishAffinity();*/
                    } else {
                        Toast.makeText(RegistrationActivity.this, "Failed",Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                progressDialog.hide();
                call.cancel();
                Toast.makeText(RegistrationActivity.this, "Failed",Toast.LENGTH_SHORT).show();
            }
        });
    }

衷心感谢

共有1个答案

暨弘懿
2023-03-14

问题在于您的locationpost类型,它应该是JSONObject,而不是JSONObject,因此请尝试以下方法之一

方法1

Api接口

Call<ResponseBody> registerUser(@Body JsonObject locationPost);
JsonObject obj = new JsonObject();
obj.addProperty("httpMethod","POST");
obj.addProperty("firstname",firstNameValue);

// add the rest of the field

Call<ResponseBody> call = apiInterface.registerUser(obj);

//rest of the logic remains same
public class RequestObject{

final String  httpMethod, firstname;
// declare member variables for all the keys

   RequestObject(String method,String firstname){
        this.httpMethod = method;
        this.firstname = firstname;
   }
}
Call<ResponseBody> registerUser(@Body RequestObject locationPost);
RequestObject requestobject = new RequestObject("POST","firstName");
// add the rest of the field

Call<ResponseBody> call = apiInterface.registerUser(requestObject);

//rest of the logic remains same
 类似资料:
  • 我正在尝试使用Postman访问API,以使用基本身份验证获取响应,但当我提交数据时,它会给我带来可怕的400错误,这显然表明某些标头设置不正确。 以下是API信息: 作为回应,我应该得到一个JSON形式的加密令牌,而不是得到这个错误。 以下是邮差截图: 我错过了什么吗?

  • 我的api基url是: https://locodealapi.herokuapp.com/api/deals 在邮递员传球,跟随在头和它的工作很好。 OKHTTP日志拦截器 注意:相同的代码在本地nodejs服务器(仅限http)中运行良好

  • 我正试着在邮递员上打API。API正在服务器上工作,但我没有收到任何数据。以下是视觉图像: 它是回应我与“没有数据”和400错误的要求。

  • 我正在使用postman客户端应用程序进行rest let的POST类型请求。对于我的URL,我传递了一个查询参数。当我将一个小JSON字符串作为值传递时,它被传递得非常好,但当我将一个大JSON字符串作为值传递时,它会抛出一个错误,错误为Bad Request 400 error。无法找出问题所在以及如何调试和修复问题。 有人能帮我吗? 谢谢你~Sha公司

  • 我正在使用volley库我的volley请求在牛轧糖,奥利奥,棉花糖操作系统工作完美,但它不工作在Lollipop设备,它给出错误服务器错误,我处理了错误,我发现错误是400坏请求,我正在分享错误屏幕截图,也分享了我的代码,请帮助我,我的api也工作在邮递员... 我的请求代码是:

  • 我有一个非常基本的webApi构建/运行(相当模板应用程序,仅此而已),以便遵循一个培训视频。GET和DELETE调用工作正常,PUT和POST给出400错误。诚然,对所有这些都很新,所以我正在寻找一些基本的方向,关于如何排除故障。相关的屏幕截图如下,我很高兴提供其他要求。提前谢了。