android获取Tomcat的JSON数据

上一篇简单的介绍获取Tomcatg根目录下的图片,本章将继续介绍进行与Tomcat通信获取目录下的jsON数据,首先在...webapps\ROOT\test目录下放入一个JSON文件,放入

categories.json了这个文件,还是利用Xutils框架与Tomcat通信,配置请看上一篇,本章将不介绍了。

通过:

HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpRequest.HttpMethod.GET, "....../test/categories.json", new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
  
}
   @Override
public void onFailure(HttpException error, String msg) {
Log.d("TAG","访问失败");
}
利用HttpUtils框架,由于API23以上放弃Http..使用,需要在build.gradle配置下:

要加上这个配置:

useLibrary 'org.apache.http.legacy' ,否则会报异常。上述省略号将自己的电脑IP地址写上就可以了,交互成功之后会进入
onSuccess...方法,将这个方法里进行解析JSON数据,解析数据需要知道:遇见“{}”就使用JSonObject,遇见“[]”就使用JsonArray。

RESONSEINFO={"retcode":200,"data":[{"id":10000,"title":"新闻","type":1,"children":[{"id":10007,"title":"北京","type":1,"url":"/10007/list_1.json"},{"id":10006,"title":"中国","type":1,"url":"/10006/list_1.json"},{"id":10008,"title":"国际","type":1,"url":"/10008/list_1.json"},{"id":10010,"title":"体育","type":1,"url":"/10010/list_1.json"},{"id":10091,"title":"生活","type":1,"url":"/10091/list_1.json"},{"id":10012,"title":"旅游","type":1,"url":"/10012/list_1.json"},{"id":10095,"title":"科技","type":1,"url":"/10095/list_1.json"},{"id":10009,"title":"军事","type":1,"url":"/10009/list_1.json"},{"id":10093,"title":"时尚","type":1,"url":"/10093/list_1.json"},{"id":10011,"title":"财经","type":1,"url":"/10011/list_1.json"},{"id":10094,"title":"育儿","type":1,"url":"/10094/list_1.json"},{"id":10105,"title":"汽车","type":1,"url":"/10105/list_1.json"}]},{"id":10002,"title":"专题","type":10,"url":"/10006/list_1.json","url1":"/10007/list1_1.json"},{"id":10003,"title":"组图","type":2,"url":"/10008/list_1.json"},{"id":10004,"title":"互动","type":3,"excurl":"","dayurl":"","weekurl":""}],"extend":[10007,10006,10008,10014,10012,10091,10009,10010,10095]}
这个是

categories.json文件下的文件内容,知道数据格式,下面就可以解析数据了。
js = new JSONObject(result);
int retcode = js.getInt("retcode");
Log.d("TAG","retcode1="+retcode);
JSONArray extend = js.getJSONArray("extend");
JSONArray data = js.getJSONArray("data");
Log.d("TAG","data="+data);
for (int i=0;i<data.length();i++){
JSONObject jsdata = data.getJSONObject(i);
Log.d("TAG","jsdata="+jsdata);
if (i==0){
int id1 = jsdata.getInt("id");
String title1 = jsdata.getString("title");
int type1 = jsdata.getInt("type");
Log.d("TAG","id1="+id1+","+"title1="+title1+"type1="+type1);
mChildrenArray = jsdata.getJSONArray("children");
Log.d("TAG","childrenArray="+mChildrenArray);
for (int j =0;j<mChildrenArray.length();j++){
JSONObject jsonObjectChlidren = mChildrenArray.getJSONObject(j);
Log.d("TAG","jsonObjectChlidren="+jsonObjectChlidren);
int id = jsonObjectChlidren.getInt("id");
Log.d("TAG","id="+id);
String title = jsonObjectChlidren.getString("title");
Log.d("TAG","title="+title);
int type = jsonObjectChlidren.getInt("type");
Log.d("TAG","type="+type);
String url = jsonObjectChlidren.getString("url");
Log.d("TAG","url12="+url);
}

}else if (i==1){
int id2 = jsdata.getInt("id");
String title2 = jsdata.getString("title");
int type2 = jsdata.getInt("type");
String url2 = jsdata.getString("url");
String url3 = jsdata.getString("url1");
Log.d("TAG","id2="+id2+","+"title2="+title2+"type2="+type2+"url2="+url2+","+"url3="+url3);
}else if (i ==2){
int id3 = jsdata.getInt("id");
String title3 = jsdata.getString("title");
int type3 = jsdata.getInt("type");
String url3 = jsdata.getString("url");
Log.d("TAG","id2="+id3+","+"title2="+title3+"type2="+type3+"url2="+url3);
}else if (i==3){
Log.d("TAG","3=");
String dayurl = jsdata.getString("dayurl");
String excurl = jsdata.getString("excurl");
int id = jsdata.getInt("id");
String title = jsdata.getString("title");
int type = jsdata.getInt("type");
String weekurl = jsdata.getString("weekurl");
Log.d("TAG","dayurl="+dayurl+","+"excurl="+excurl+"id="+id+"title="+title+"type="+type+","+"weekurl="+weekurl);
}

}


} catch (Exception e) {
e.printStackTrace();
}

这样就可以将数据全部解析出来。解析JSoN数据还有其他方法,这就不一一介绍了,方法原理都是这样的。
 

posted @ 2017-06-21 16:48  三飞  阅读(2352)  评论(0编辑  收藏  举报