Sub*_*abu 3 post android bad-request magento retrofit2
我想使用 JSON 数据(提供 JSON 作为 JsonObject)在 Retrofit 中调用 POST 方法(Magento REST API)。为此,我从邮递员那里打电话给我,对我来说工作正常。
我已经完成了 android 部分如下,
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
);}
Run Code Online (Sandbox Code Playgroud)
APIUtility 类为
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);
}
Run Code Online (Sandbox Code Playgroud)
}
RetrofitClient 类为
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;
}
Run Code Online (Sandbox Code Playgroud)
最后,调用函数如下
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) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
带有主体的 POST 方法的 CallRetrofit API。
将您的代码更改为这样的内容,GsonConverterFactory 将 User 对象转换为 json 本身。
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);
}
Run Code Online (Sandbox Code Playgroud)
这是用户类:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5612 次 |
| 最近记录: |