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:

  1. public class HttpUtils {   //从服务器端下载到Json数据,也就是个字符串   
  2.   
  3.     public static String getData(String url) throws Exception {   
  4.   
  5.         StringBuilder sb = new StringBuilder();   
  6.   
  7.         HttpClient httpClient = new DefaultHttpClient();   
  8.   
  9.         HttpGet httpGet = new HttpGet(url);   
  10.   
  11.         HttpResponse httpResponse = httpClient.execute(httpGet);   
  12.   
  13.         HttpEntity httpEntity = httpResponse.getEntity();   
  14.   
  15.         if (httpEntity != null) {   
  16.   
  17.             InputStream instream = httpEntity.getContent();   
  18.   
  19.             BufferedReader reader = new BufferedReader(new InputStreamReader(   
  20.   
  21.                     instream));   
  22.   
  23.             String line = null;   
  24.   
  25.             while ((line = reader.readLine()) != null) {   
  26.   
  27.                 sb.append(line);   
  28.   
  29.             }   
  30.   
  31.             return sb.toString();   
  32.   
  33.         }   
  34.   
  35.         return null;   
  36.   
  37.     }  


JsonUtils.java:

  1. public class JsonUtils {   
  2.   
  3.     public static List<Student> parseStudentFromJson(String data) {   
  4.   
  5.         Type listType = new TypeToken<LinkedList<Student>>() {   
  6.   
  7.         }.getType();   
  8.   
  9.         Gson gson = new Gson();   
  10.   
  11.         LinkedList<Student> list = gson.fromJson(data, listType);   
  12.   
  13.         return list;   
  14.   
  15.     }   
  16.   
  17. }  

里面的Student是一个JavaBean对象:

  1. public class Student {   
  2.   
  3.     private String name;   
  4.   
  5.     private int age;   
  6.   
  7.     private String id;   
  8.   
  9.     
  10.   
  11.     public Student() {   
  12.   
  13.         super();   
  14.   
  15.     }   
  16.   
  17.     
  18.   
  19.     public Student(String name, int age, String id) {   
  20.   
  21.         super();   
  22.   
  23.         this.name = name;   
  24.   
  25.         this.age = age;   
  26.   
  27.         this.id = id;   
  28.   
  29.     }   
  30.   
  31.     
  32.   
  33.     public String getName() {   
  34.   
  35.         return name;   
  36.   
  37.     }   
  38.   
  39.     
  40.   
  41.     public void setName(String name) {   
  42.   
  43.         this.name = name;   
  44.   
  45.     }   
  46.   
  47.     
  48.   
  49.     public int getAge() {   
  50.   
  51.         return age;   
  52.   
  53.     }   
  54.   
  55.     
  56.   
  57.     public void setAge(int age) {   
  58.   
  59.         this.age = age;   
  60.   
  61.     }   
  62.   
  63.     
  64.   
  65.     public String getId() {   
  66.   
  67.         return id;   
  68.   
  69.     }   
  70.   
  71.     
  72.   
  73.     public void setId(String id) {   
  74.   
  75.         this.id = id;   
  76.   
  77.     }   
  78.   
  79.     
  80.   
  81. }  

再看看我们要解析网络数据的activity代码

  1. public class MainActivity extends Activity {   
  2.   
  3.     private TextView textView;   
  4.   
  5.     private List<Student> list;   
  6.   
  7.     
  8.   
  9.     /** Called when the activity is first created. */   
  10.   
  11.     @Override   
  12.   
  13.     public void onCreate(Bundle savedInstanceState) {   
  14.   
  15.         super.onCreate(savedInstanceState);   
  16.   
  17.         setContentView(R.layout.main);   
  18.   
  19.         textView = (TextView) findViewById(R.id.textView);   
  20.   
  21.         String data = null;   
  22.   
  23.         try {   
  24.   
  25.             data = HttpUtils   
  26.   
  27.                     .getData("http://10.16.12.165:8080/JsonTest/JsonTestServlet");   
  28.   
  29.         } catch (Exception e) {   
  30.   
  31.             e.printStackTrace();   
  32.   
  33.         }   
  34.   
  35.         String result = "";   
  36.   
  37.         list = JsonUtils.parseStudentFromJson(data);   
  38.   
  39.         for (Student s : list) {   
  40.   
  41.             result += "name: " + s.getName() + "   " + "age: " + s.getAge()   
  42.   
  43.                     + "   " + "id: " + s.getId() + "\n";   
  44.   
  45.         }   
  46.   
  47.         textView.setText(result);   
  48.   
  49.     }   
  50.   
  51. }  

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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值