使用 Json 数据改造 POST 方法得到错误代码 400:错误请求 [英] Retrofit POST Method with Json data got error code 400 : Bad Request

查看:34
本文介绍了使用 Json 数据改造 POST 方法得到错误代码 400:错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 JSON 数据(提供 JSON 作为 JsonObject)在 Retrofit 中调用 POST 方法(Magento REST API).为此,我从邮递员那里打电话给我,对我来说工作正常.

I would like to call a POST Method(Magento REST API) in Retrofit with a JSON data(provide JSON as JsonObject). For that, I call as follow from the postman and work fine for me.

我已经完成了android部分如下,

I have done the android parts as follow,

API 接口

public interface APIService {

@POST("seq/restapi/checkpassword")
@Headers({
        "Content-Type: application/json;charset=utf-8",
        "Accept: application/json;charset=utf-8",
        "Cache-Control: max-age=640000"
})
Call<Post> savePost(
        @Body JSONObject jsonObject
);}

APIUtility 类为

APIUtility class as

public class ApiUtils {

private ApiUtils() {
}

public static final String BASE_URL = "http://xx.xxxx.xxx.xxx/api/rest/";
public static APIService getAPIService() {

    return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}

}

RetrofitClient 类为

RetrofitClient class as

private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

最后,调用函数如下

public void sendPost(JSONObject jsonObject) {

    Log.e("TEST", "******************************************  jsonObject" + jsonObject);
    mAPIService.savePost(jsonObject).enqueue(new Callback<Post>() {
        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {
            if (response.isSuccessful()) {

            }
        }

        @Override
        public void onFailure(Call<Post> call, Throwable t) {

        }
    });
}

使用带有主体的 POST 方法调用 Retrofit API.

CallRetrofit API with the POST method with a body.

推荐答案

将您的代码更改为这样的内容,GsonConverterFactory 会将 User 对象转换为 json 本身.

Change your code to something like this, GsonConverterFactory converts a User object to json itself.

public interface APIService {
    @POST("seq/restapi/checkpassword")
    @Headers({
        "Content-Type: application/json;charset=utf-8",
        "Accept: application/json;charset=utf-8",
        "Cache-Control: max-age=640000"
    })
    Call<Post> savePost(@Body User user);
}

这是用户类:

public class User {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

这篇关于使用 Json 数据改造 POST 方法得到错误代码 400:错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆