Retrofit自定义解析器

某些情况下,从服务器返回过来的json无法自动解析,只能通过手动解析(比如json某些字段的key是不固定的),如果项目中又正在用Retrofit作为网络库的话,就会比较头疼,因为Retrofit一般都只需要调用.addConverterFactory()传一个GsonConverterFactory对象就可以用Gson完成自动解析。然而网络上对于Retrofit的资源相当有限,摸索了一段时间终于知道如何用Retrofit自定义解析器了,写下来做个备忘,也为了方便后来人。

通常情况下Retrofit的代码会是这样:

private static OkHttpClient.Builder sOkHttpClient = new OkHttpClient.Builder();
private static Converter.Factory sGsonConverterFactory = GsonConverterFactory.create();
private static CallAdapter.Factory sRxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();

public static HotApi getHotApi(){
    if(sHotApi == null){
        Retrofit retrofit = new Retrofit.Builder()
                .client(sOkHttpClient.build())
                .baseUrl(BASE_URL)
                .addConverterFactory(sGsonConverterFactory)//这里一般传个GsonConverterFactory对象就可以用gson完成自动解析
                .addCallAdapterFactory(sRxJavaCallAdapterFactory)
                .build();
        sHotApi = retrofit.create(HotApi.class);
    }
    return sHotApi;
}

但是如果不想用Gson完成自动解析则需要自定义解析器了,总共分为四步。
步骤一:

public abstract class BaseResponseConverter implements Converter {

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

    public abstract T parserJson(String json);
}

public abstract class BaseConverterFactory extends Converter.Factory {
    @Override
    public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return responseConverter();
    }

    public abstract BaseResponseConverter responseConverter();

    @Override
    public Converter requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
    }
}

步骤二:

public class HotResponseConverter extends BaseResponseConverter{

    @Override
    public HotListBean parserJson(String json) {
        HotListBean bean = new HotListBean();
        try {
            JSONObject jsonRoot = new JSONObject(json);
            String status = jsonRoot.getString("status");
            bean.setStatus(status);
            if ("ok".equals(status)) {
                JSONObject jsonPkg = jsonRoot.getJSONObject("list");
                bean.setId(jsonPkg.getString("id"));
                bean.setName(jsonPkg.getString("name"));
                bean.setType(jsonPkg.getString("type"));

                JSONArray JAcontents = jsonPkg.getJSONArray("contents");
                List contents = new ArrayList<>();
                for (int i = 0; i < JAcontents.length(); i++) {
                    contents.add(JAcontents.getString(i));
                }
                bean.setContents(contents);

                bean.setTimestamp(jsonPkg.getLong("timestamp"));
                bean.setCount(jsonPkg.getInt("count"));

                JSONObject jsonDetails = jsonPkg.getJSONObject("details");
                List details = new ArrayList<>();
                for (String emoId:contents) {
                    details.add(new Gson().fromJson(jsonDetails.getJSONObject(emoId).toString(), EmoticonBean.class));
                }
                bean.setDetails(details);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return bean;
    }
}

步骤三:

public interface HotApi {
    @GET("v2_5/lists/hot")
    Observable getHots(@Query("auth_token") String authToken,@Query("user_id")String userId,@Query("limit")int limit,@Query("page") int page);
}

步骤四:

public static HotApi getHotApi(){
    if(sHotApi == null){
        Retrofit retrofit = new Retrofit.Builder()
                .client(sOkHttpClient.build())
                .baseUrl(BASE_URL)
                .addConverterFactory(new BaseConverterFactory() {
                    @Override
                    public BaseResponseConverter responseConverter() {
                        return new HotResponseConverter();
                    }
                })
                .addCallAdapterFactory(sRxJavaCallAdapterFactory)
                .build();
        sHotApi = retrofit.create(HotApi.class);
    }
    return sHotApi;
}

你可能感兴趣的:(Retrofit自定义解析器)