APIjson多層資料實體類封裝(利用泛型技術)

雲驛站發表於2016-04-20

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qingfeng812/article/details/51199835

完整原始碼檢視點選進入:github地址 
 

1: API json多層資料實體類封裝(利用泛型技術)

image

image

RootEntity.java

public class RootEntity<T>{

private int showapi_res_code;
private String showapi_res_error;
private PageBeanBody<T> showapi_res_body;
//省略get set方法
}

PageBeanBody.java

public class PageBeanBody<T> {

    private PageBean<T> pagebean;
    private int ret_code;
     //省略get set方法
}

PageBean.java

public class PageBean<T> {

private int allNum; 
private int allPages;
private int currentPage;
private int maxResult;
private List<T> contentlist;
//省略get set方法
}

泛型方法

 public static <T> RootEntity<T> parseNewsList(Response response,Class<T> mClazz) {
        JSONObject root=JSON.parseObject(response.getResponseText());
        JSONObject body=JSON.parseObject(response.getResponseText()).getJSONObject("showapi_res_body");
        JSONObject page=JSON.parseObject(response.getResponseText()).getJSONObject("showapi_res_body").getJSONObject("pagebean");
        String contentList=JSON.parseObject(response.getResponseText()).getJSONObject("showapi_res_body").getJSONObject("pagebean").getJSONArray("contentlist").toJSONString();
        //例項化分頁類
        PageBean<T> pageBean=new PageBean<T>();
        pageBean.setContentlist(JSON.parseArray(contentList, mClazz));//可能報錯
        pageBean.setAllNum(page.getIntValue("allNum"));
        pageBean.setAllPages(page.getIntValue("allPages"));
        pageBean.setCurrentPage(page.getIntValue("currentPage"));
        pageBean.setMaxResult(page.getIntValue("maxResult"));
        //例項化Body類
        PageBeanBody<T> pageBeanBody=new PageBeanBody<T>();
        pageBeanBody.setPagebean(pageBean);
        pageBeanBody.setRet_code(body.getIntValue("ret_code"));
        //例項化Root類
        RootEntity<T> rootEntity=new RootEntity<T>();
        rootEntity.setShowapi_res_body(pageBeanBody);
        rootEntity.setShowapi_res_code(root.getIntValue("showapi_res_code"));
        rootEntity.setShowapi_res_error(root.getString("showapi_res_error"));
        return rootEntity;
    }

呼叫泛型方法

public static void loadNewsList(String url, Map<String, Object> param, LinkedHashMap<String, Object> headers,
            String bodyJson, String post) {
        Response response = StaticUtil.commomHttpMethod(url, param, headers, bodyJson, post);
        //呼叫泛型方法 
        RootEntity<NewEntity> data= parseNewsList(response, NewEntity.class);
        System.out.println("desc:"+data.getShowapi_res_body().getPagebean().getContentlist().get(0).getImageurls().size());
        System.out.println("code:"+data.getShowapi_res_code());
        System.out.println("error:"+data.getShowapi_res_error());
        System.out.println("allnum:"+data.getShowapi_res_body().getPagebean().getAllNum());
        System.out.println("allpages:"+data.getShowapi_res_body().getPagebean().getAllPages());
        System.out.println("currentpages:"+data.getShowapi_res_body().getPagebean().getCurrentPage());
        System.out.println("maxresult:"+data.getShowapi_res_body().getPagebean().getMaxResult());
    }


相關文章