我们先看下前面写的接口
Call<String> login( @Body User ueser);
如果返回的是Observable格式,则这么定义接口:
Observable<String> login(@Body User ueser);
从上面可以看到,
Retrofit接口的返回值分为两部分,一部分是前面的Call或者Observable,另一部分是String。
addCallAdapterFactory影响的就是第一部分的Call或者Observable,Call类型是默认支持的(内部由DefaultCallAdapterFactory支持),而如果要支持Observable,我们就需要自己添加
addCallAdapterFactory(RxJavaCallAdapterFactory.create())
CallAdapter会影响具体怎么执行这条请求
addConverterFactory影响的就是第二部分以及我们的请求参数,如上面的User,我们可以看下BuiltInConverters的responseBodyConverter
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
if (type == ResponseBody.class) {
if (Utils.isAnnotationPresent(annotations, Streaming.class)) {
return StreamingResponseBodyConverter.INSTANCE;
}
return BufferingResponseBodyConverter.INSTANCE;
}
if (type == Void.class) {
return VoidResponseBodyConverter.INSTANCE;
}
return null;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
if (RequestBody.class.isAssignableFrom(Types.getRawType(type))) {
return RequestBodyConverter.INSTANCE;
}
return null;
}
它只支持返回值的第二部分是ResponseBody.class和Void.class类型的或者请求参数为ResponseBody.class,我们代码中还添加了ScalarsConverterFactory和GsonConverterFactory
分别看下它们的responseBodyConverter
@Override public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
if (type == String.class
|| type == boolean.class
|| type == Boolean.class
|| type == byte.class
|| type == Byte.class
|| type == char.class
|| type == Character.class
|| type == double.class
|| type == Double.class
|| type == float.class
|| type == Float.class
|| type == int.class
|| type == Integer.class
|| type == long.class
|| type == Long.class
|| type == short.class
|| type == Short.class) {
return ScalarRequestBodyConverter.INSTANCE;
}
return null;
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit)