【问题标题】:Retrofit 2.0 response failed and return null for throwback if used Realm object encapsulation如果使用 Realm 对象封装,Retrofit 2.0 响应失败并返回 null 以进行回退
【发布时间】:2015-11-22 08:22:58
【问题描述】:

您好,我是 Retrofit 和 Realm for Android 的新手,所以如果有任何愚蠢的代码,请多多包涵。

所以在这种情况下我有一个简单的 API 用于登录,这是我代码中的 User 对象:

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 个对象。

我有什么方法可以将 1 Object 用于 Retrofit 和 Realm 实现,或者我的代码中是否有任何错误或遗漏? 非常感谢。

【问题讨论】:

    标签: android retrofit realm


    【解决方案1】:

    您需要像这里描述的那样配置 GSON:https://realm.io/docs/java/latest/#retrofit

    虽然我对 Retrofit 2 不是很熟悉,而且看起来 API 略有变化,但我希望你能明白。

    【讨论】:

    • 是的,我尝试过您提到的 GSON 初始化,但它忽略了我的用户对象上的 @SerializedName。 GSON 有什么方法可以运行@SerializedName?。非常感谢
    【解决方案2】:

    所以@Christian 提到使用 GSON 和这个链接 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 时遇到困难的人!

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2012-10-26
      • 2020-08-17
      • 2012-11-03
      • 1970-01-01
      • 2016-02-25
      相关资源
      最近更新 更多