2016-06-14 37 views
0

我正在閱讀一些在stackoverflow中的另一篇文章,但我不能得到它的工作。如何使用Retrofit2 + GSON - >具有自定義對象的類?

我有服務器返回的數據,具有一個表格:

服務器響應

{ 
    "userData": { 
    "username": "victor.javier", 
    "email": "[email protected]", 
    "createdAt": "2016-05-11T09:55:14.720Z", 
    "updatedAt": "2016-05-11T09:55:14.720Z", 
    "id": "5733018274ddfad25" 
    }, 
    "token": "SkpaNuXdzfrhiH06qGK93EH2ujM37hfk02F8o2EodYJumG" 
} 

我的代碼POJO是下一:

User.class

public class User { 

    @SerializedName("userData") 
    @Expose 
    private UserData userData; 

    @SerializedName("token") 
    @Expose 
    private String token; 

    ... CONSTRUCTOR AND GETTERS/SETTERS 

} 

UserData.class

public class UserData { 

    @SerializedName("username") 
    @Expose 
    private String username; 

    @SerializedName("email") 
    @Expose 
    private String email; 

    @SerializedName("createdAt") 
    @Expose 
    private String createdAt; 

    @SerializedName("updatedAt") 
    @Expose 
    private String updatedAt; 

    @SerializedName("id") 
    @Expose 
    private String id; 

    ... CONSTRUCTOR AND GETTERS/SETTERS 

} 

我從另一篇文章複製隔壁班的反序列化GSON。該代碼是:

RestDeserializer.class

public class RestDeserializer<T> implements JsonDeserializer<T> { 

    private Class<T> mClass; 
    private String mKey; 

    public RestDeserializer(Class<T> targetClass, String key) { 
     mClass = targetClass; 
     mKey = key; 
    } 

    @Override 
    public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) 
      throws JsonParseException { 
     JsonElement content = je.getAsJsonObject().get(mKey); 
     return new Gson().fromJson(content, mClass); 

    } 
} 

改裝的實現是:

RetroFitImpl

public class UserApiImpl implements UserApi { 

    @Inject 
    public UserApiImpl() { } 

    @Override 
    public User loginUser(String identifier, String password) { 
     Retrofit retrofitBuilder = getRetrofitBuilder(); 
     UserRetrofitApi userRetrofitApi = retrofitBuilder.create(UserRetrofitApi.class); 

     LoginRequestEntity loginRequestEntity = new LoginRequestEntity(); 
     loginRequestEntity.setIdentifier(identifier); 
     loginRequestEntity.setPassword(password); 

     Call<User> call = userRetrofitApi.loginUser(loginRequestEntity); 
     Response<User> response = null; 
     try { 
      response = call.execute(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     User body = null; 
     if (response != null) { 
      body = response.body(); 
     } 
     return body; 
    } 

    private Retrofit getRetrofitBuilder() { 
     HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
     interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
     OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); 

     return new Retrofit.Builder() 
       .baseUrl(BuildConfig.API_URL) 
       .client(client) 
       .addConverterFactory(buildGsonConverter()) 
       .build(); 
    } 

    private static GsonConverterFactory buildGsonConverter() { 
     GsonBuilder gsonBuilder = new GsonBuilder(); 

     Gson gson = new GsonBuilder() 
       .registerTypeAdapter(User.class, new RestDeserializer<>(User.class, "userData")) 
       .create(); 

     return GsonConverterFactory.create(gson); 
    } 
} 

我做得不對的buildGsonConverter方法因爲我每次都會打電話給onError進行翻新,所以我認爲這是GSON建設中的一個問題。

任何人都可以告訴我如何解析這個構建一個GSON像服務器的響應?

謝謝。

回答

0

檢查從改造文檔的例子

http://square.github.io/retrofit/

你並不需要創建自己的工廠GSON

.addConverterFactory(GsonConverterFactory.create()) 

而且也增加的build.gradle

com.squareup.retrofit2:converter-gson 
+0

我已經刪除了該行,但這不是解決方案。 :(。謝謝保羅 – MAOL

+0

你不必刪除你的線,你需要使用gson轉換器工廠作爲例子! –

相關問題