使用OKHttp请求json数据一直显示null

问题描述

在使用Okhttp3+retrofit2+Rxjava请求json数据时,可以正常请求数据,但是在转换成实体类时,一直显示null,具体问题如下:

D/OkHttp: { "error_code" : 0 , "reason" : "Success!" , "result" : { "GetRankTypeRsp" : { "RankList" : { "Rank" : [ { "rankTypeID" : "1" , "rankName" : "点击排行"} , { "rankTypeID" : "2" , "rankName" : "搜索排行"} , { "rankTypeID" : "3" , "rankName" : "收藏排行"} , { "rankTypeID" : "4" , "rankName" : "人气排行"}]} , "RankTimeList" : { "RankTime" : [ { "rankTimeID" : "1" , "rankTimeName" : "日排行"} , { "rankTimeID" : "2" , "rankTimeName" : "周排行"} , { "rankTimeID" : "3" , "rankTimeName" : "月排行"} , { "rankTimeID" : "4" , "rankTimeName" : "总排行"}]} , "ContentTypeList" : { "ContentType" : [ { "contentTypeID" : "1" , "contentType" : "图书"} , { "contentTypeID" : "2" , "contentType" : "杂志"} , { "contentTypeID" : "3" , "contentType" : "漫画"}]}}}}
D/OkHttp: <-- END HTTP (784-byte body)
D/onNext: 0,Result{getRankTypeRsp=null}
D/BasePresenter-->: requestSuccess()
D/OneFragment: loadSuccess------------Result{getRankTypeRsp=null}
D/BasePresenter-->: requestComplete()

问题分析

从上面null异常说明肯定是json格式转换实体类时出现了问题,于是去检查了下ApiMananger类,相关代码如下:

   /**
     * 初始化Retrofit
     */
    public ApiManager() {
        initOkhttpClient();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
              //gson和java类相互转化      .addConverterFactory(GsonConverterFactory.create())
                .client(mOkHttpClient)
                .build();
        //实例化api接口
        mApiService = retrofit.create(ApiService.class);
    }

确定addConverterFactory()无误后,猜想可能是实体类有问题。
先看下实体类的相互关系

使用OKHttp请求json数据一直显示null_第1张图片
最底部HttpResult是通用的http实体类

public class HttpResult {
    private int errorCode;
    private String reason;
    private T result;

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "HttpResult{" +
                "errorCode=" + errorCode +
                ", reason='" + reason + '\'' +
                ", result=" + result +
                '}';
    }
}

HttpResult中的result对应着json数据源对象,也就是下面的Result类

public class Result extends BaseEntity.BaseBean {

    private GetRankTypeRsp getRankTypeRsp;

    @Override
    public String toString() {
        return "Result{" +
                "getRankTypeRsp=" + getRankTypeRsp +
                '}';
    }

    public GetRankTypeRsp getGetRankTypeRsp() {
        return getRankTypeRsp;
    }

    public void setGetRankTypeRsp(GetRankTypeRsp getRankTypeRsp) {
        this.getRankTypeRsp = getRankTypeRsp;
    }
}

Result类中的getRankTypeRsp包含另一个json数据源对象,该类如下:

public class GetRankTypeRsp extends BaseEntity.BaseBean {
    private RankList RankList;
    private RankTimeList rankTimeList;
    private ContentTypeList contentTypeList;

    public com.ninoyang.geek.model.rankType.RankList getRankList() {
        return RankList;
    }

    public void setRankList(com.ninoyang.geek.model.rankType.RankList rankList) {
        RankList = rankList;
    }

    public RankTimeList getRankTimeList() {
        return rankTimeList;
    }

    public void setRankTimeList(RankTimeList rankTimeList) {
        this.rankTimeList = rankTimeList;
    }

    public ContentTypeList getContentTypeList() {
        return contentTypeList;
    }

    public void setContentTypeList(ContentTypeList contentTypeList) {
        this.contentTypeList = contentTypeList;
    }

    @Override
    public String toString() {
        return "GetRankTypeRsp{" +
                "RankList=" + RankList +
                ", rankTimeList=" + rankTimeList +
                ", contentTypeList=" + contentTypeList +
                '}';
    }
}

然后再对比一下json数组中

{ "error_code" : 0 , "reason" : "Success!" , "result" : { "GetRankTypeRsp" : { "RankList....省略部分json}}

发现json数组中的GetRankTypeRsp和上面Result类中的getRankTypeRsp大小写不一样,又可能是getxxx和系统中的get方法冲突了,于是尝试把实体类中getRankTypeRsp换成GetRankTypeRsp,然后重新生成了gettter和setter方法,发现居然不报nulll了!!!!!

最终解决方法

使java类中的字段名和json数据中的字段名保持一致,包括大小写。

你可能感兴趣的:(使用OKHttp请求json数据一直显示null)