1. 程式人生 > >Android獲取伺服器Json資料與Json的解析

Android獲取伺服器Json資料與Json的解析

由於有第三方框架的存在,從伺服器獲取Json資料以及解析變得非常非常的簡單。

1 獲取Json資料

第一個第三方框架是xUtils的HttpUtils:

在Global包中定義了一些全域性靜態變數類,用於儲存訪問路徑,在這裡電腦分配給我模擬器的IP為:192.168.56.1:8080

則路徑定義是:

package com.example.zhihuibj.global;
public class GlobalContants {

	public static final String SERVICE_URL="http://192.168.56.1:8080/zhbj/";
	public static final String CATEGORIES_URL=SERVICE_URL+"categories.json";//獲取分類資訊

	
}

使用Get方法獲取網路資料使用HttpUtils:
/*
	 * 獲取網路資料
	 */
	private void GetDataFromService() {
		HttpUtils utils=new HttpUtils();
		utils.send(HttpMethod.GET, GlobalContants.CATEGORIES_URL,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						String result= responseInfo.result;
						System.out.println("返回結果"+result);
						parseData(result);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(mAcitivty, msg, Toast.LENGTH_SHORT).show();
						error.printStackTrace();
						System.out.println("解析錯誤!!!");
					}
		});
		
	}

所有的資料Json資料會儲存在result中,並在自定義的parseData()中進行解析。

獲取資料就是這麼簡單。

2 Json資料解析

對於資料的解析使用Google提供的 Jar包: gson-2.3.1.jar

這個jar包提供了一個只需要傳入用於儲存Json資料的自定義類,即可自動完成所有資料的解析。

下面是伺服器端的Json資料:

{
    "data": [
        {
            "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": 10000,
            "title": "新聞",
            "type": 1
        },
        {
            "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"
        },
        {
            "dayurl": "",
            "excurl": "",
            "id": 10004,
            "title": "互動",
            "type": 3,
            "weekurl": ""
        }
    ],
    "extend": [
        10007,
        10006,
        10008,
        10014,
        10012,
        10091,
        10009,
        10010,
        10095
    ],
    "retcode": 200
}


對於這個資料自定義一個儲存類,以C語言的角度來講,就是用一個結構體來儲存資料:

package com.example.zhihuibj.domain;

import java.util.ArrayList;

/*
 * 網路分類資訊封裝
 * 
 * 欄位必須和伺服器返回的欄位相同,方便gson解析
 */
public class NewsData {

	
	public int retcode;
	public ArrayList<NewsMenuData> data;
	
	public class NewsMenuData{
		
		
		@Override
		public String toString() {
			return "NewsMenuData [id=" + id + ", title=" + title + ", type=" + type + ", url=" + url + ", children="
					+ children + "]";
		}
		public String id;
		public String title;
		public int type;
		public String url;
		//public String url1;
		public ArrayList<NewsTabData> children;
		
		
	}
		
	public class NewsTabData{
		
		public String id;
		public String title;
		public int type;
		public String url;		
		
	}
	
	
	
}

最後呼叫parasData解析資料即可:
	/*
	 * 解析網路資料
	 */
	protected void parseData(String result) {
		Gson gson =new Gson();
		 NewsData data = gson.fromJson(result, NewsData.class);//直接填入類名
		
		 System.out.println("解析結果"+data);
	}