2016-10-19 75 views
0

我從服務器此JSON響應:改造2 GSON多態性JSON解析

{ 
"Information": { 
"Id": "2dbc0dad8df94f7de7b63d8f22a03c8f", 
"Type": "User", 
"Name": "ASD", 
"IsInProgress": false 
}, 
"Errors": [] 
} 

但有時反應是這樣的:

{ 
"Information": { 
"Id": "2dbc0dad8df94f7ca6b66d5f22a03c8f", 
"Type": "Organization", 
"Name": "ASD", 
"City": "London" 
"Street": "Wall street" 
}, 
"Errors": [] 
} 

我想與改造2解析這個+ Gson。 我創建了自定義的多態轉換器廠:

public class DetailConverterFactory extends Converter.Factory { 

private Gson gson; 
@Override 
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { 

    if (gson == null){ 
     final RuntimeTypeAdapterFactory<DetailInformation> typeFactory = RuntimeTypeAdapterFactory 
       .of(DetailInformation.class, "Type") // Here you specify which is the parent class and what field particularizes the child class. 
       .registerSubtype(User.class, "User"); 
.registerSubtype(Organization.class, "Organization"); 

     // add the polymorphic specialization 
     gson = new GsonBuilder().registerTypeAdapterFactory(typeFactory).create(); 
    } 

    TypeAdapter<?> gsonAdapter = gson.getAdapter(TypeToken.get(type)); 
    return new CustomResponseBodyConverter<>(gsonAdapter, gson); //super.responseBodyConverter(type, annotations, retrofit); 
} 

private class CustomResponseBodyConverter<T> implements Converter<ResponseBody, T>{ 

    private TypeAdapter<T> gsonAdapter; 
    private Gson gson; 

    public CustomResponseBodyConverter(TypeAdapter<T> gsonAdapter, Gson gson) { 
     this.gsonAdapter = gsonAdapter; 
     this.gson = gson; 
    } 

    @Override 
    public T convert(ResponseBody value) throws IOException { 

     JsonReader jsonReader = gson.newJsonReader(value.charStream()); 

     try{ 
      return gsonAdapter.read(jsonReader); 
     } finally { 
      value.close(); 
     } 
    } 
} 
} 

我的問題是,我不知道如何使用它。 我已經將我的自定義轉換器工廠添加到我的restclient。我打電話給我的API是這樣的:

Callback<DetailModel> detailModelCallback = new Callback<DetailModel>() { 
     @Override 
     public void onResponse(Call<DetailModel> call, Response<DetailModel> response) { 
      final DetailModel detailResponse = new RetrofitObjectExtractor<>(DetailModel.class).extract(response); 

      if (detailResponse == null){ 
       // TODO error handling 
       Log.d("ERR", " detail null :("); 
      }else { 
       Log.d("OK", "good"); 
      } 
     } 

     @Override 
     public void onFailure(Call<DetailModel> call, Throwable t) { 
      Log.d("ERR", "fail"); 
     } 

但我DetailModel是一個基類,我User.class和Organization.class是DetailModel的孩子。但我不知道如何解析對我的用戶或組織模型的響應。

回答

0

使用retrofit2 + GSON解析響應,就像這樣:

String baseurl="http://"; 
    Retrofit retrofit = new Retrofit 
      .Builder() 
      .addConverterFactory(GsonConverterFactory.create()) 
      .baseUrl(baseurl) 
      .build(); 

    HttpApi httpApi = retrofit.create(HttpApi.class); 
    Call<DetailModel> call= httpApi.getData(); 
    call.enqueue(new Callback<DetailModel>() { 
     @Override 
     public void onResponse(Call<DetailModel> call, Response<DetailModel> response) { 
      //todo 
     } 

     @Override 
     public void onFailure(Call<DetailModel> call, Throwable t) { 
      //todo 
     } 
    }); 

的DetailModel:

public class DetailModel { 

public InformationBean Information; 

public List<?> Errors; 

public static class InformationBean { 
    public String Id; 
    public String Type; 
    public String Name; 
    public String City; 
    public String Street; 
    public boolean IsInProgress; 
} 

}

的HttpApi:

interface HttpApi { 

@GET("home") 
Call<DetailModel> getData(); 

}