Android利用Json來進行網絡數據傳輸 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

最近做一項目,有很多地方得用到網絡數據傳輸與解析,這裡采用的是Json方式,它與傳統的XML解析方式比起來,有自己的一些優點,首先,它是比XML更輕量級,再一個,寫一個XML文件是個煩人的事兒,而Json則相對輕松些。

          Android平臺有Jsong相關的類來進行Json數據解析,悲劇的是,它們是Android SDK3.0以後才能用的。不過在谷歌網站:http://code.google.com/p/google-gson/裡有一個名為Gson的類庫,可以用它來解析Json數據,並且,Adroid 3.0平臺裡其實也就是把這一部分直接整合進Android裡瞭。我們要解析Json數據,直接去網站上下載個jar包,導入到工程裡,就可以解析Json數據瞭。

下面有個例子,很清晰的解釋瞭這種工作方式:

先看看兩個我自己封裝的類:

HttpUtils.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;

    }

 

 

JsonUtils.java:

 

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;

    }

}

裡面的Student是一個JavaBean對象:

 

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;

    }

 

}

再看看我們要解析網絡數據的Activity:

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);

    }

}

這樣就可以獲取網絡數據並加以解析利用瞭,運行結果如下:

 

 

另外,還有一篇不錯的文章:/kf/201110/109534.html

 

作者 StoneBreaker

發佈留言