如果使用 Realm 对象封装,Retrofit 2.0 响应失败并且 return 返回 null
Retrofit 2.0 response failed and return null for throwback if used Realm object encapsulation
嗨,我是 Android 的 Retrofit 和 Realm 的新手,所以如果有任何愚蠢的代码,请多多包涵。
所以在这种情况下我有一个简单的 API 用于登录,这是我代码中的用户对象:
public class User extends RealmObject {
@SerializedName("id")
private String ID;
@SerializedName("username")
private String username;
@SerializedName("token_type")
private String tokenType;
@SerializedName("token")
private String token;
//Setter and Getter goes here
}
登录API界面:
public interface LoginAPI {
@POST("api/v1/login")
Call<User> login(@Body JsonObject user);
}
这是对我的登录请求的实现 API :
//OnClickListener
View.OnClickListener loginButtonClicked = new View.OnClickListener() {
public void onClick(View v) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("username", emailEditText.getText().toString());
jsonObject.addProperty("password", passwordEditText.getText().toString());
Retrofit loginRequest = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
LoginAPI loginAPI = loginRequest.create(LoginAPI.class);
Call<User> call = loginAPI.login(jsonObject);
call.enqueue(LoginFragment.this);
}
};
//API Callback
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
User user = response.body();
Realm realm = Realm.getInstance(getContext());
realm.beginTransaction();
realm.copyToRealmOrUpdate(user);
realm.commitTransaction();
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(getActivity(), "Failed " + t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
问题是它总是调用 onFailure 回调并为 Throwable t 值返回 NULL。
当我的用户对象没有扩展 RealmObject 时,我的代码可以工作,但这意味着我必须为 Retrofit 和 Realm 创建 2 个对象。
有什么方法可以让我在 Retrofit 和 Realm 实现中使用 1 个对象,或者我的代码中是否有任何错误或遗漏?
非常感谢。
您需要按照此处所述配置 GSON:https://realm.io/docs/java/latest/#retrofit
不过我对 Retrofit 2 不是很熟悉,看起来 API 略有变化,但我希望你能理解。
所以@Christian 提到了使用 GSON 和这个 link here
,最后我得到了这个与 @Expose
.
一起工作
这就是我现在的代码:
用户对象
public class User extends RealmObject {
@Expose
@SerializedName("id")
private String ID;
@Expose
@SerializedName("username")
private String username;
@Expose
@SerializedName("token_type")
private String tokenType;
@Expose
@SerializedName("token")
private String token;
//Setter and Getter goes here
}
登录API界面:
public interface LoginAPI {
@POST("api/v1/login")
Call<JsonObject> login(@Body JsonObject user);
}
API 我的登录回调 API :
//API Callback
@Override
public void onResponse(Response<JsonObject> response, Retrofit retrofit) {
JsonObject results = response.body();
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Realm realm = Realm.getInstance(getContext());
realm.beginTransaction();
User user = gson.fromJson(results.toString(), User.class);
realm.copyToRealm(user);
realm.commitTransaction();
}
现在它的工作就像一个魅力。希望这能帮助那些在代码中集成 Retrofit 和 Realm 有困难的人!
嗨,我是 Android 的 Retrofit 和 Realm 的新手,所以如果有任何愚蠢的代码,请多多包涵。
所以在这种情况下我有一个简单的 API 用于登录,这是我代码中的用户对象:
public class User extends RealmObject {
@SerializedName("id")
private String ID;
@SerializedName("username")
private String username;
@SerializedName("token_type")
private String tokenType;
@SerializedName("token")
private String token;
//Setter and Getter goes here
}
登录API界面:
public interface LoginAPI {
@POST("api/v1/login")
Call<User> login(@Body JsonObject user);
}
这是对我的登录请求的实现 API :
//OnClickListener
View.OnClickListener loginButtonClicked = new View.OnClickListener() {
public void onClick(View v) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("username", emailEditText.getText().toString());
jsonObject.addProperty("password", passwordEditText.getText().toString());
Retrofit loginRequest = new Retrofit.Builder()
.baseUrl(Constant.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
LoginAPI loginAPI = loginRequest.create(LoginAPI.class);
Call<User> call = loginAPI.login(jsonObject);
call.enqueue(LoginFragment.this);
}
};
//API Callback
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
User user = response.body();
Realm realm = Realm.getInstance(getContext());
realm.beginTransaction();
realm.copyToRealmOrUpdate(user);
realm.commitTransaction();
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(getActivity(), "Failed " + t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
问题是它总是调用 onFailure 回调并为 Throwable t 值返回 NULL。
当我的用户对象没有扩展 RealmObject 时,我的代码可以工作,但这意味着我必须为 Retrofit 和 Realm 创建 2 个对象。
有什么方法可以让我在 Retrofit 和 Realm 实现中使用 1 个对象,或者我的代码中是否有任何错误或遗漏? 非常感谢。
您需要按照此处所述配置 GSON:https://realm.io/docs/java/latest/#retrofit
不过我对 Retrofit 2 不是很熟悉,看起来 API 略有变化,但我希望你能理解。
所以@Christian 提到了使用 GSON 和这个 link here
,最后我得到了这个与 @Expose
.
这就是我现在的代码:
用户对象
public class User extends RealmObject {
@Expose
@SerializedName("id")
private String ID;
@Expose
@SerializedName("username")
private String username;
@Expose
@SerializedName("token_type")
private String tokenType;
@Expose
@SerializedName("token")
private String token;
//Setter and Getter goes here
}
登录API界面:
public interface LoginAPI {
@POST("api/v1/login")
Call<JsonObject> login(@Body JsonObject user);
}
API 我的登录回调 API :
//API Callback
@Override
public void onResponse(Response<JsonObject> response, Retrofit retrofit) {
JsonObject results = response.body();
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Realm realm = Realm.getInstance(getContext());
realm.beginTransaction();
User user = gson.fromJson(results.toString(), User.class);
realm.copyToRealm(user);
realm.commitTransaction();
}
现在它的工作就像一个魅力。希望这能帮助那些在代码中集成 Retrofit 和 Realm 有困难的人!