【发布时间】:2018-06-03 10:53:31
【问题描述】:
我是 Retrofit 的新手。 我只是使用改造向我的数据库添加一行,但我在我的 api 界面中使用了字段参数,就像这样。
Call<String> matchWith(@Field("fbid") String fbid, @Field("matchwith") String matchwith);
很难为大对象编写,所以我决定使用@Body 注释。但现在它在我的 mysql 数据库中发布空行/对象。 (使用 api 接口中的@Field 注释我的代码正确插入数据)
这是我的代码
@POST("/insertuser.php")
Call<User> postUser(@Body User user);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myurl.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
Connect connect = retrofit.create(Connect.class);
Call<User> call = connect.postUser(user);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
在我的对象类中,我使用了 @SerializedName 和 @Expose 改造仍然插入空我尝试过仅使用 @SerilizedName 并仅使用 @Expose 之后我什么也没使用,但我得到了相同的结果。
【问题讨论】:
-
会不会是'user'是空的?
-
没有用户对象不为空。
标签: android gson retrofit retrofit2