Android利用Json来进行网络数据传输

 地方得用到网络数据传输与解析,这里采用的是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);

    }

}

这样就可以获取网络数据并加以解析利用了,运行结果如下:

 

 

另外,还有一篇不错的文章:http://www.2cto.com/kf/201110/109534.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值