| 
目前有很多地方得用到網路數據傳輸與解析,這裡採用的是Json方式,它與傳統的XML解析方式比起來,有自己的一些優點,首先,它是比XML更輕量級,再一個,寫一個XML文件是個煩人的事兒,而Json則相對輕鬆些。
x
馬上加入Android 台灣中文網,立即免費下載應用遊戲。您需要 登錄 才可以下載或查看,沒有帳號?註冊  
 Android平台有Jsong相關的類來進行Json數據解析,悲劇的是,它們是Android SDK3.0以後才能用的。不過在Google網站: http://code.google.com/p/google-gson/裡有一個名為Gson的類庫,可以用它來解析Json數據,並且,Adroid 3.0平台裡其實也就是把這一部分直接整合進Android裡了。我們要解析Json數據,直接去網站上下載個jar包,導入到工程裡,就可以解析Json數據了。
 
 下面有個例子,很清晰的解釋了這種工作方式:
 
 先看看兩個封裝的類:
 HttpUtils.java:
 JsonUtils.java:複製代碼public class HttpUtils {   //從服務器端下載到Json數據,也就是個字符串
    public static String getData(String url) throws Exception {
        StringBuilder sb = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            InputStream instream = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    instream));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        }
        return null;
    }
裡面的Student是一個JavaBean對像:複製代碼public class JsonUtils {
    public static List<Student> parseStudentFromJson(String data) {
        Type listType = new TypeToken<LinkedList<Student>>() {
        }.getType();
        Gson gson = new Gson();
        LinkedList<Student> list = gson.fromJson(data, listType);
        return list;
    }
}
再看看我們要解析網路數據的Activity:複製代碼public class Student {
    private String name;
    private int age;
    private String id;
 
    public Student() {
        super();
    }
 
    public Student(String name, int age, String id) {
        super();
        this.name = name;
        this.age = age;
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
}
這樣就可以獲取網路數據並加以解析利用了複製代碼public class MainActivity extends Activity {
    private TextView textView;
    private List<Student> list;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView);
        String data = null;
        try {
            data = HttpUtils
                    .getData("http://10.16.12.165:8080/JsonTest/JsonTestServlet");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = "";
        list = JsonUtils.parseStudentFromJson(data);
        for (Student s : list) {
            result += "name: " + s.getName() + "   " + "age: " + s.getAge()
                    + "   " + "id: " + s.getId() + "\n";
        }
        textView.setText(result);
    }
}
 |