2015-11-22 17 views
2

嗨,我是Android的新用戶,因此如果有任何愚蠢的代碼,請耐心等待。Retrofit 2.0響應失敗,如果使用Realm對象封裝,則返回null for throwback

所以我在這種情況下登錄一個簡單的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,但這意味着我必須創建2對象Retrofit和領域。

有沒有什麼辦法可以讓我使用1個對象來進行Retrofit和Realm實現,或者我的代碼中是否有任何錯誤或遺漏? 非常感謝。

回答

0

因此,@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接口:我登錄API

public interface LoginAPI { 
    @POST("api/v1/login") 
    Call<JsonObject> login(@Body JsonObject user); 
} 

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整合到他們的代碼中!

0

您需要配置GSON喜歡這裏描述:https://realm.io/docs/java/latest/#retrofit

我不是那個熟悉的改造2,雖然,它看起來像API已經略有改變,但我希望你的想法。

+0

是的我嘗試了像你提到的GSON初始化,但它忽略了我的用戶對象上的'@ SerializedName'。有什麼方法可以讓GSON運行'@ SerializedName'?真的謝謝 – KidouKid

相關問題