2016-02-05 51 views
1

如何解決它?改裝2 beta-4。 Android 6.無法創建轉換器

Process: www.palchiki.com.palchiki, PID: 2047 
java.lang.IllegalArgumentException: Unable to create converter for class www.palchiki.com.palchiki.model.ServiceResponse 
for method APIWebService.getServices 
at retrofit2.Utils.methodError(Utils.java:154) 
at retrofit2.MethodHandler.createResponseConverter(MethodHandler.java:62) 
at retrofit2.MethodHandler.create(MethodHandler.java:33) 
at retrofit2.Retrofit.loadMethodHandler(Retrofit.java:164) 
at retrofit2.Retrofit$1.invoke(Retrofit.java:145) 
at java.lang.reflect.Proxy.invoke(Proxy.java:393) 
at $Proxy2.getServices(Unknown Source) 
at www.palchiki.com.palchiki.fragment.ServiceFragment.onCreateView(ServiceFragment.java:94) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 

我使用com.squareup.retrofit2:改型:2.0.0-BETA4和com.squareup.retrofit2:轉換器-GSON:2.0.0-BETA4

適配器

Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(URLWebService) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .client(okHttpClient) 
      .build(); 

接口

@GET("/integration.php?command=getServices") 
Call<ServiceResponse> getServices(@Query("city") String cityId); 

回答

0

您必須爲類ServiceResponse指定一個Converter,以便gson序列化/反序列化您的類效果沉着應對 它會是這樣的

GsonBuilder builder = new GsonBuilder(); 
builder.registerTypeAdapter(ServiceResponse.class, new ServiceResponseJson()); 
Gson gson = builder.build(); 

以後,你就必須提供此GSON實例您GSON轉換器廠這樣

....addConverterFactory(GsonConverterFactory.create(gson)) 

ServiceResponseJson將必須實現 JsonDeserializer和/或JsonSerializer

+0

GSON gsonBuilder =新GsonBuilder()excludeFieldsWithModifiers(Modifier.FINAL,Modifier.TRANSIENT,Modifier.STATIC) .serializeNulls() .create(); –

0

在Android M中使用Retrofit2時有一個isssue,如here中提到的

之一在於爲我工作的解決方案是:

Retrofit retrofit = new Retrofit.Builder() 
     .baseUrl(URLWebService) 
     .addConverterFactory(GsonConverterFactory.create(
           new GsonBuilder() 
           .excludeFieldsWithoutExposeAnnotation() 
           .create() 
          )) 
     .client(okHttpClient) 
     .build();