某些情況下钞诡,從服務(wù)器返回過來的json無法自動(dòng)解析,只能通過手動(dòng)解析(比如json某些字段的key是不固定的)湃崩,如果項(xiàng)目中又正在用Retrofit作為網(wǎng)絡(luò)庫的話荧降,就會(huì)比較頭疼,因?yàn)镽etrofit一般都只需要調(diào)用.addConverterFactory()傳一個(gè)GsonConverterFactory對(duì)象就可以用Gson完成自動(dòng)解析攒读。然而網(wǎng)絡(luò)上對(duì)于Retrofit的資源相當(dāng)有限朵诫,摸索了一段時(shí)間終于知道如何用Retrofit自定義解析器了,寫下來做個(gè)備忘薄扁,也為了方便后來人剪返。
通常情況下Retrofit的代碼會(huì)是這樣:
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)//這里一般傳個(gè)GsonConverterFactory對(duì)象就可以用gson完成自動(dòng)解析
.addCallAdapterFactory(sRxJavaCallAdapterFactory)
.build();
sHotApi = retrofit.create(HotApi.class);
}
return sHotApi;
}
但是如果不想用Gson完成自動(dòng)解析則需要自定義解析器了废累,總共分為四步。
步驟一:
public abstract class BaseResponseConverter<T> implements Converter<ResponseBody,T> {
@Override
public T convert(ResponseBody value) throws IOException {
return parserJson(value.string());
}
public abstract T parserJson(String json);
}
public abstract class BaseConverterFactory<T> extends Converter.Factory {
@Override
public Converter<ResponseBody, T> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return responseConverter();
}
public abstract BaseResponseConverter<T> responseConverter();
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
}
}
步驟二:
public class HotResponseConverter extends BaseResponseConverter<HotListBean>{
@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<String> 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<EmoticonBean> 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<HotListBean> 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;
}