刚开始学Retrofit的时候,就不去用复杂的Converter了。想先用下基本款String的Converter:ScalarsConverterFactory。于是敲下一下代码:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(MyConstant.baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
结果发现找不到ScalarsConverterFactory这个类,明明已经引入了retrofit的包。回去看官方文档,发现有这么一段话:
Converters can be added to support other types. Six sibling modules adapt popular serialization libraries for your convenience.
Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
好家伙,原来Converter居然是独立包引入的,还有这种操作。去Maven repository搜一下converter-scalars,把依赖加上。
<!-- Converter的依赖 -->
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-scalars</artifactId>
<version>2.3.0</version>
</dependency>
问题解决。