Retrofit(六) StringConverterFactory

我们知道,想要怎么解析http传输的内容,可以在Converter里定义好。这句话是Converter的核心思想。

今天讲的StringConverterFactory,要达到效果, 就像下面这个那样,返回Call<String>, 或者 Observer<String>, 泛型具体化的类都是String

是把内容以String发送出去,解析成String返回给接口方法。

public interface IFileTransferService {
    @POST("v1/info/pair")
    Call<String> pairDevice(@HeaderMap Map<String, String> heads, @Body Device device);

    @POST("v1/info/pair")
    Observable<String> pairDeviceObservable(@HeaderMap Map<String, String> heads, @Body Device device);
}

首先, 继承Converter.Factory。实现两个request/response converter的工厂方法。

responseBodyConverter是response converter的工厂方法。

requestBodyConverter是request converter的工厂方法。

这里有一步很关键,需要注意。一定要添加下面的代码。不然遍历这个Converter的时候,即使不适合也会执行。 不是你能Converter的就返回null。GsonConverter就有这个坑。

        if(type != String.class){
            return null;
        }
public class StringConverterFactory extends Converter.Factory {
    public static Converter.Factory create() {
        return new StringConverterFactory();
    }

    public @Nullable Converter<ResponseBody, String> responseBodyConverter(
            Type type, Annotation[] annotations, Retrofit retrofit) {

        if(type != String.class){
            return null;
        }

        return new StringResponseBodyConverter();
    }

    public @Nullable Converter<String, RequestBody> requestBodyConverter(
            Type type,
            Annotation[] parameterAnnotations,
            Annotation[] methodAnnotations,
            Retrofit retrofit) {
  
        if(type != String.class){
            return null;
        }

        return new StringRequestBodyConverter();
    }

    public @Nullable Converter<String, String> stringConverter(
            Type type, Annotation[] annotations, Retrofit retrofit) {
        return null;
    }
}

其次,实现StringResponseBodyConverter。把response body的内容,解析成String, 直接返回出去, 不做任何操作,不想GSON Converter会解析成对象返回出去。

public class StringResponseBodyConverter implements Converter<ResponseBody, String> {

    @Override
    public String convert(ResponseBody value) throws IOException {
        return value.string();
    }
}

最后, 实现StringRequestBodyConverter, 接受String做为resquest body内容。

public class StringRequestBodyConverter implements Converter<String, RequestBody > {
    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");

    @Override
    public RequestBody convert(String value) throws IOException {
        Buffer buffer = new Buffer();
        Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
        writer.write(value);
        writer.close();

        return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
    }
}

写这一篇的目的, 不是说这个Converter Factory多有用。 只是为了加深对Converter的理解,以及灵活使用。

其实Square公司已经提供了String Converter的类,ScalarsConverterFactory.java。   

    implementation 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'

下一篇再讲一下,BitmapConverterFactory

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值