解析
@Override
public void parseResult(String result, int code, IResultCallBackListener linstener)
{
if (handlerException(result, mHandler, InteractiveConstant.QUERY_CITY_INFO_FAILED,
InteractiveConstant.QUERY_PROVINCE_INFO_EMPTY))
{
linstener.onEmpty();
return;
}
JSonObject jsonObject = null;
JSonObject jsonData = null;
JSonArray jsonArray = null;
JSonObject object = null;
try
{
jsonData = new JSonObject(result);
String type = "";
String resultCode = "";
if (jsonData.has("type"))
{
type = jsonData.getString("type");
}
if (jsonData.has("code"))
{
resultCode = jsonData.getString("code");
}
if (TextUtils.isEmpty(resultCode) || !resultCode.equals("0"))
{
//没有查询到结果
linstener.onEmpty();
return;
}
if (!TextUtils.isEmpty(type) && type.equals("7"))
{
if (jsonData.has("dialogueID"))
{
Constants.DIALOGUE_ID = jsonData.getString("dialogueID");
}
else
{
Constants.DIALOGUE_ID = "";
}
if (jsonData.has("data"))
{
JSonArray jsonArray1 = jsonData.getJSonArray("data");
ResolverInfo.Data datas = new ResolverInfo().new Data();
if (jsonArray1 != null || jsonArray1.length() > 0)
{
for (int m = 0; m < jsonArray1.length(); m++)
{
JSonObject json1 = jsonArray1.getJSonObject(m);
if (json1.has("html"))
{
//解压html
// String html = ZLibUtils.decompressAndbase64(json1.getString("html"));
String htmlString = json1.getString("html");
// htmlString = htmlString
// .replaceAll("\"\[", "[")
// .replaceAll("\]\"", "]")
// .replaceAll("\\\\"", "'")
// .replaceAll("\\"", """);
htmlString = htmlString
.replaceAll("\r\n", "")
.replaceAll("\\\"", "'");
JSonObject htmlJson = new JSonObject(htmlString);
ResponseInfo info = ResponseInfo.parseToBean(htmlJson, true);
//将解析出的info树结构转化成list结构
List responseInfoList = ResponseInfo.transformToList(info);
if (responseInfoList != null && responseInfoList.size() > 0)
{
DBManager.getInstance(baseApplication.getAppContext()).getProblemTreeDB()
.deleteAll();
DBManager.getInstance(baseApplication.getAppContext()).getProblemTreeDB()
.insert(responseInfoList);
}
}
List paths = new ArrayList<>();
if (json1.has("name"))
{
String pathstring = json1.getString("name");
JSonArray pathArray = new JSonArray(pathstring);
if (pathArray != null && pathArray.length() > 0)
{
for (int j = 0; j < pathArray.length(); j++)
{
String value = pathArray.getString(j);
if (!TextUtils.isEmpty(value) && !value.equals("-") && !value.equals("null"))
{
paths.add(pathArray.getString(j));
}
}
}
}
if (paths != null && paths.size() > 0)
{
linstener.onSuccess(paths);
}
else
{
linstener.onEmpty();
}
}
}
}
}
else
{
//没有查询到结果
linstener.onEmpty();
}
}
catch (JSonException e)
{
GCLogger.error(Module.HTTP_UTIL, "[" + TAG + "]" + "[HttpsRequest] parseResult Exception " + e.toString());
linstener.onFail("");
}
}
Bean
package com.huawei.genexcloud.icare.bean;
import android.text.TextUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class ResponseInfo
{
//定位思路
private String analysis_direction;
//专家案例
private String expert_case;
//标识id
private String id;
//信息采集
private String info_collection;
private String isroot;
//解决方案
private String recommend_solution;
//标题
private String topic;
//定位工具
private String troubleshoot_tool;
//父节点id
private String parentid;
//子节点
private List children;
public static ResponseInfo parseToBean(JSonObject jsonObject, boolean isRoot) {
if(null == jsonObject) {
return null;
}
ResponseInfo info = new ResponseInfo();
if(isRoot) {
info.setIsroot("1");
}
//解析child之前需要先解析自己的id,以便在解析child时提供
info.setId(jsonObject.optString("id"));
info.setTopic(jsonObject.optString("topic"));
JSonArray child = jsonObject.optJSonArray("children");
if(null != child && child.length() > 0) {
info.setChildren(parseToList(child, info));
}
return info;
}
public static List parseToList(JSonArray jsonArray, ResponseInfo parent) {
List data = new ArrayList<>();
for(int i=0; i transformToList(ResponseInfo info) {
//如果info没有id或者不是根节点,则无需解析
if(null == info || TextUtils.isEmpty(info.getId()) || TextUtils.isEmpty(info.getIsroot())) {
return null;
}
List infoList = new ArrayList<>();
addToList(info, infoList);
return infoList;
}
private static void addToList(ResponseInfo info, List list) {
if(null == info || TextUtils.isEmpty(info.getId())) {
return;
}
String isRoot = info.getIsroot();
String parentId = info.getParentid();
//当info为根节点或者 有parentid的子节点,才是正常的info
if(!TextUtils.isEmpty(isRoot) || (TextUtils.isEmpty(isRoot) && !TextUtils.isEmpty(parentId))) {
list.add(info);
}
List child = info.getChildren();
if(null == child || child.size() == 0) {
return;
} else {
for(int i=0; i getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public String getAnalysis_direction()
{
return analysis_direction;
}
public void setAnalysis_direction(String analysis_direction)
{
this.analysis_direction = analysis_direction;
}
public String getExpert_case()
{
return expert_case;
}
public void setExpert_case(String expert_case)
{
this.expert_case = expert_case;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getInfo_collection()
{
return info_collection;
}
public void setInfo_collection(String info_collection)
{
this.info_collection = info_collection;
}
public String getIsroot()
{
return isroot;
}
public void setIsroot(String isroot)
{
this.isroot = isroot;
}
public String getRecommend_solution()
{
return recommend_solution;
}
public void setRecommend_solution(String recommend_solution)
{
this.recommend_solution = recommend_solution;
}
public String getTopic()
{
return topic;
}
public void setTopic(String topic)
{
this.topic = topic;
}
public String getTroubleshoot_tool()
{
return troubleshoot_tool;
}
public void setTroubleshoot_tool(String troubleshoot_tool)
{
this.troubleshoot_tool = troubleshoot_tool;
}
public String getParentid()
{
return parentid;
}
public void setParentid(String parentid)
{
this.parentid = parentid;
}
}



