Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在改装中获取错误代码400错误请求,但对邮递员有效_Android_Api_Retrofit_Retrofit2_Okhttp - Fatal编程技术网

Android 在改装中获取错误代码400错误请求,但对邮递员有效

Android 在改装中获取错误代码400错误请求,但对邮递员有效,android,api,retrofit,retrofit2,okhttp,Android,Api,Retrofit,Retrofit2,Okhttp,我正在制作一个注册API 它将json输入作为follow/request参数 {"httpMethod":"POST", "firstname":"Ali", "lastname":"Patel", "email":"alipatel05@gmail.com", "password":"12345678&q

我正在制作一个注册API 它将json输入作为follow/request参数

{"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;
    }
}
我的API接口

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);

}
Call<ResponseBody> registerUser(@Body JsonObject locationPost);
Call<ResponseBody> registerUser(@Body RequestObject locationPost);
公共接口{
@标题({
“内容类型:application/json;charset=utf-8”,
“Accept:application/json;charset=utf-8”,
“缓存控制:无缓存”
})
@后(“疫苗接种”)
调用registerUser(@Body JSONObject locationPost);
}
这是我的api调用

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();
            }
        });
    }
private void registerUser(){
HashMap newhashMap=新HashMap();
JSONObject hashMap=新的JSONObject();
试一试{
hashMap.put(“httpMethod”、“POST”);
put(“firstname”,mEditFirstName.getText().toString().trim());
put(“lastname”,mEditLastName.getText().toString().trim());
put(“email”,mEditEmail.getText().toString().trim());
hashMap.put(“密码”,mEditPassword.getText().toString().trim());
hashMap.put(“国家”、“加拿大”);
hashMap.put(“州”、“魁北克”);
hashMap.put(“城市”,mSelectedCity);
put(“type”,mUserType);
}捕获(JSONException e){
e、 printStackTrace();
}
Call Call=apinterface.registerUser(hashMap);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
progressDialog.hide();
试一试{
JSONObject=newJSONObject(response.body().toString());
e(“SignupFragment”,jsonObject.toString());
if(response.code()==200){
Toast.makeText(RegistrationActivity.this,“Success”,Toast.LENGTH_SHORT.show();
/*意向意向=新意向(RegistrationActivity.this、LoginActivity.class);
星触觉(意向);
有限性()*/
}否则{
Toast.makeText(RegistrationActivity.this,“Failed”,Toast.LENGTH_SHORT.show();
}
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
progressDialog.hide();
call.cancel();
Toast.makeText(RegistrationActivity.this,“Failed”,Toast.LENGTH_SHORT.show();
}
});
}
我是否需要更改接口,或者我的api调用中可能存在一些错误


感谢andvance

我认为您必须仔细重新检查这些参数

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

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

方法1

Api接口

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);

}
Call<ResponseBody> registerUser(@Body JsonObject locationPost);
Call<ResponseBody> registerUser(@Body RequestObject locationPost);
API接口

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);

}
Call<ResponseBody> registerUser(@Body JsonObject locationPost);
Call<ResponseBody> registerUser(@Body RequestObject locationPost);
callregisteruser(@Body-RequestObject-locationPost);
Api调用

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
RequestObject requestobject = new RequestObject("POST","firstName");
// add the rest of the field

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

//rest of the logic remains same
RequestObject RequestObject=newrequestobject(“POST”、“firstName”);
//添加字段的其余部分
Call Call=apiInterface.registerUser(请求对象);
//其余的逻辑保持不变

我在浏览器中打开你的url,得到了这个
{“消息”:“内部服务器错误”}
这是
500
错误代码不是
400
,在你的浏览器中试试这个是否回答了你的问题@miladsalimi这是因为您直接调用api时没有正文{“httpMethod”:“POST”,“firstname”:“Ali”,“lastname”:“Patel”,“email”:alipatel05@gmail.com“,”密码“:”12345678“,”国家“:”加拿大“,”州“:”魁北克“,”城市“:”蒙特利尔“,”类型“:”家长“}这是api主体调用,在postman中可以,但在android中不行app@PrajwalWaingankar不,我使用的是原始数据请求,请检查我的问题。@AliPatel检查答案,看看它是否解决了您的问题谢谢您的回答您的方法1帮助了我。很高兴您的问题解决了,快乐编码:)不,我用的是JSONObject而不是JSONObject。这是唯一的问题。无论如何,谢谢你的回复。