Retrofit(2.0)入門小錯誤 -- Could not locate ResponseBody xxx Tried: * retrofit.BuiltInConverters

前言

照例給出官網:Retrofit官網java

學習資料:https://speakerdeck.com/jakewharton/simple-http-with-retrofit-2-droidcon-nyc-2015git

其實你們學習的時候,徹底能夠按照官網Introduction,本身寫一個例子來運行。可是百密一疏,官網可能忘記添加了一句很是重要的話,致使你可能出現以下錯誤:github

Could not locate ResponseBody converterjson

錯誤信息:
Caused by: java.lang.IllegalArgumentException: Could not locate ResponseBody converter for java.util.List<com.bzh.sampleretrofit.Repo>. Tried:
* retrofit.BuiltInConverters
at retrofit.Retrofit.responseConverter(Retrofit.java:266)
at retrofit.MethodHandler.createResponseConverter(MethodHandler.java:55)
... 18 more

能夠知道,是因爲響應的結果在由Json轉化爲對象時出了錯誤。api

 

解決

首先在gradle文件中引入依賴庫。學習

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' #使用Gson轉化json串

其次在構建Retrofit對象時,指明使用的Converter工廠。gradle

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://api.github.com")
        .addConverterFactory(GsonConverterFactory.create()) // 加上這句話
        .build();
相關文章
相關標籤/搜索