美文网首页网络Android知识Android开发
Retrofit 自定义Converter.Factory实现直

Retrofit 自定义Converter.Factory实现直

作者: j春雨 | 来源:发表于2016-09-09 21:23 被阅读4731次

前言

在使用Retrofit过程中,通过服务器获取的数据,不一定是标准的json数据,当时就想能不能有一种方式,可以把数据直接获取到而不是解析好的数据

开始实现

主要是实现MediaType,以及responseBodyConverter和requestBodyConverter方法;

public class ToStringConverterFactory extends Converter.Factory {
    private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");


    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        if (String.class.equals(type)) {
            return new Converter<ResponseBody, String>() {
                @Override
                public String convert(ResponseBody value) throws IOException {
                    return value.string();
                }
            };
        }
        return null;
    }

    @Override public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
            Annotation[] methodAnnotations, Retrofit retrofit) {
        if (String.class.equals(type)) {
            return new Converter<String, RequestBody>() {
                @Override
                public RequestBody convert(String value) throws IOException {
                    return RequestBody.create(MEDIA_TYPE, value);
                }
            };
        }
        return null;
    }
}

开始使用

public interface WechatService {

    @GET("/txapi/weixin/wxhot") Call<String> getHotArticleStr(@Header("apikey") String apiKey,
            @Query("num") int num, @Query("rand") int rand, @Query("word") String word, @Query("page") int page,
            @Query("src") String src);
}
public void getHotArticleRxString(int num, int rand, String word, int page, String src) {
        Retrofit retrofit = new Retrofit
                .Builder()
                .addConverterFactory(new ToStringConverterFactory())
                .baseUrl(baseurl).build();
        WechatService service = retrofit.create(WechatService.class);
        Call<String> resultObser = service.getHotArticleStr(baiduApiKey, num, rand, word, page, src);
        resultObser.enqueue(new Callback<String>() {
            @Override public void onResponse(Call<String> call, Response<String> response) {
                Log.e("jiangcy", "ToStringConverterFactory : " + response.body().toString());
            }

            @Override public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

demo 地址

相关文章

网友评论

  • 微特米:感谢 帮助到我了
  • hjiangwen:可以把Call对象的泛型指定为ResponseBody,就可以在response直接取得String response了。
    ```
    //Service类
    @get("/txapi/weixin/wxhot")
    Call<ResponseBody> getHotArticleStr() ;

    //在回调时
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
    Log.e("jiangcy", "StringResponse : " + response.body().string());
    }



    ```
    j春雨: @Jiangwen_ 我试一下,多谢指教

本文标题:Retrofit 自定义Converter.Factory实现直

本文链接:https://www.haomeiwen.com/subject/kbfyettx.html