Json解析实例Tomcat服务器

Person类


[java]  view plain copy
  1. package com.jsonMysql.person;  
  2.   
  3. public class Person {  
  4.     private String name;  
  5.     private String address;  
  6.     private Integer age;  
  7.   
  8.     public Person() {  
  9.         super();  
  10.     }  
  11.   
  12.     public Person(String name, String address, Integer age) {  
  13.         super();  
  14.         this.name = name;  
  15.         this.address = address;  
  16.         this.age = age;  
  17.     }  
  18.   
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.   
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.   
  27.     public String getAddress() {  
  28.         return address;  
  29.     }  
  30.   
  31.     public void setAddress(String address) {  
  32.         this.address = address;  
  33.     }  
  34.   
  35.     public Integer getAge() {  
  36.         return age;  
  37.     }  
  38.   
  39.     public void setAge(Integer age) {  
  40.         this.age = age;  
  41.     }  
  42.   
  43.     @Override  
  44.     public String toString() {  
  45.         return "Person [name=" + name + ", address=" + address + ", age=" + age  
  46.                 + "]";  
  47.     }  
  48.       
  49.       
  50.   
  51. }  

MainActivity类

[java]  view plain copy
  1. package com.jsonMysql.main;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import com.jiangqq.csdn.R;  
  8. import com.jsonMysql.person.Person;  
  9. import com.jsonMysql.util.JsonParse;  
  10.   
  11. import android.app.Activity;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.ListView;  
  18. import android.widget.SimpleAdapter;  
  19. import android.widget.Toast;  
  20.   
  21. public class MainActivity extends Activity {  
  22.     private Button mButton;  
  23.     private ListView mListView;  
  24.     //使用IP不能使用localhost或者127.0.0.1,因为android模拟器默认绑定这个IP,这里应该访问局域网IP  
  25.     private static final String urlPath = "http://192.168.1.101:8080/JsonWeb/JsonServlet";  
  26.     private static final String TAG = "MainActivity";  
  27.     private List<Person> persons;  
  28.   
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.         mButton = (Button) findViewById(R.id.button1);  
  34.         mListView = (ListView) findViewById(R.id.listView1);  
  35.         mButton.setOnClickListener(new MyOnClickListener());  
  36.     }  
  37.   
  38.     private class MyOnClickListener implements OnClickListener {  
  39.         @Override  
  40.         public void onClick(View v) {  
  41.             try {  
  42.                 // 得到Json解析成功之后数据  
  43.                 persons = JsonParse.getListPerson(urlPath);  
  44.                 List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();  
  45.                 for (int i = 0; i < persons.size(); i++) {  
  46.                     HashMap<String, Object> map = new HashMap<String, Object>();  
  47.                     map.put("name", persons.get(i).getName());  
  48.                     map.put("address", persons.get(i).getAddress());  
  49.                     map.put("age", persons.get(i).getAge());  
  50.                     data.add(map);  
  51.                 }  
  52.                 SimpleAdapter _Adapter = new SimpleAdapter(MainActivity.this,  
  53.                         data, R.layout.listview_item, new String[] { "name",  
  54.                                 "address""age" }, new int[] { R.id.textView1,  
  55.                                 R.id.textView2, R.id.textView3 });  
  56.                 mListView.setAdapter(_Adapter);  
  57.             } catch (Exception e) {  
  58.                 Toast.makeText(MainActivity.this"解析失败"2000);  
  59.                 Log.i(TAG, e.toString());  
  60.   
  61.             }  
  62.         }  
  63.     }  
  64. }  


Json解析类


[java]  view plain copy
  1. package com.jsonMysql.util;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. import java.nio.charset.Charset;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. import org.json.JSONArray;  
  12. import org.json.JSONObject;  
  13.   
  14. import com.jsonMysql.person.Person;  
  15.   
  16. public class JsonParse {  
  17.     /** 
  18.      * 解析Json数据 
  19.      *  
  20.      * @param urlPath 
  21.      * @return mlists 
  22.      * @throws Exception 
  23.      */  
  24.   
  25.     public static List<Person> getListPerson(String urlPath) throws Exception {  
  26.         List<Person> mlists = new ArrayList<Person>();  
  27.         byte[] data = readParse(urlPath);  
  28.         JSONArray array = new JSONArray(new String(data));  
  29.         for (int i = 0; i < array.length(); i++) {  
  30.             JSONObject item = array.getJSONObject(i);  
  31.             String name = item.getString("name");  
  32.             String address = item.getString("address");  
  33.             int age = item.getInt("age");  
  34.             mlists.add(new Person(name, address, age));  
  35.         }  
  36.         return mlists;  
  37.     }  
  38.   
  39.     /** 
  40.      * 从指定的url中获取字节数组 
  41.      *  
  42.      * @param urlPath 
  43.      * @return 字节数组 
  44.      * @throws Exception 
  45.      */  
  46.     public static byte[] readParse(String urlPath) throws Exception {  
  47.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  48.         byte[] data = new byte[1024];  
  49.         int len = 0;  
  50.         URL url = new URL(urlPath);  
  51.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  52.         InputStream inStream = conn.getInputStream();  
  53.   
  54.         while ((len = inStream.read(data)) != -1) {  
  55.             outStream.write(data, 0, len);  
  56.   
  57.         }  
  58.         inStream.close();  
  59.         return outStream.toByteArray();  
  60.   
  61.     }  
  62. }  

Mainifest.xml


[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.jiangqq.csdn"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name="com.jsonMysql.main.MainActivity"  
  14.             android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23.     <uses-permission android:name="android.permission.INTERNET" />  
  24.   
  25. </manifest>  

list.xml

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:paddingLeft="15dip"  
  12.         android:text="TextView" />  
  13.   
  14.     <TextView  
  15.         android:id="@+id/textView3"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:paddingLeft="15dip"  
  19.         android:text="TextView" />  
  20.   
  21.     <TextView  
  22.         android:id="@+id/textView2"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:paddingLeft="15dip"  
  26.         android:text="TextView" />  
  27.   
  28. </LinearLayout>  

main.xml

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="@string/jsonname" />  
  10.     <Button  
  11.         android:id="@+id/button1"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="获取数据并且解析" />  
  15.     <ListView  
  16.         android:id="@+id/listView1"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content" >  
  19.     </ListView>  
  20. </LinearLayout>  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值