Json解析实例Tomcat服务器

Person类


package com.jsonMysql.person;

public class Person {
	private String name;
	private String address;
	private Integer age;

	public Person() {
		super();
	}

	public Person(String name, String address, Integer age) {
		super();
		this.name = name;
		this.address = address;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", address=" + address + ", age=" + age
				+ "]";
	}
	
	

}

MainActivity类

package com.jsonMysql.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jiangqq.csdn.R;
import com.jsonMysql.person.Person;
import com.jsonMysql.util.JsonParse;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button mButton;
	private ListView mListView;
	//使用IP不能使用localhost或者127.0.0.1,因为android模拟器默认绑定这个IP,这里应该访问局域网IP
	private static final String urlPath = "http://192.168.1.101:8080/JsonWeb/JsonServlet";
	private static final String TAG = "MainActivity";
	private List<Person> persons;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mButton = (Button) findViewById(R.id.button1);
		mListView = (ListView) findViewById(R.id.listView1);
		mButton.setOnClickListener(new MyOnClickListener());
	}

	private class MyOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			try {
				// 得到Json解析成功之后数据
				persons = JsonParse.getListPerson(urlPath);
				List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
				for (int i = 0; i < persons.size(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("name", persons.get(i).getName());
					map.put("address", persons.get(i).getAddress());
					map.put("age", persons.get(i).getAge());
					data.add(map);
				}
				SimpleAdapter _Adapter = new SimpleAdapter(MainActivity.this,
						data, R.layout.listview_item, new String[] { "name",
								"address", "age" }, new int[] { R.id.textView1,
								R.id.textView2, R.id.textView3 });
				mListView.setAdapter(_Adapter);
			} catch (Exception e) {
				Toast.makeText(MainActivity.this, "解析失败", 2000);
				Log.i(TAG, e.toString());

			}
		}
	}
}


Json解析类


package com.jsonMysql.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.jsonMysql.person.Person;

public class JsonParse {
	/**
	 * 解析Json数据
	 * 
	 * @param urlPath
	 * @return mlists
	 * @throws Exception
	 */

	public static List<Person> getListPerson(String urlPath) throws Exception {
		List<Person> mlists = new ArrayList<Person>();
		byte[] data = readParse(urlPath);
		JSONArray array = new JSONArray(new String(data));
		for (int i = 0; i < array.length(); i++) {
			JSONObject item = array.getJSONObject(i);
			String name = item.getString("name");
			String address = item.getString("address");
			int age = item.getInt("age");
			mlists.add(new Person(name, address, age));
		}
		return mlists;
	}

	/**
	 * 从指定的url中获取字节数组
	 * 
	 * @param urlPath
	 * @return 字节数组
	 * @throws Exception
	 */
	public static byte[] readParse(String urlPath) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		URL url = new URL(urlPath);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		InputStream inStream = conn.getInputStream();

		while ((len = inStream.read(data)) != -1) {
			outStream.write(data, 0, len);

		}
		inStream.close();
		return outStream.toByteArray();

	}
}

Mainifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jiangqq.csdn"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.jsonMysql.main.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="15dip"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="15dip"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="15dip"
        android:text="TextView" />

</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/jsonname" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取数据并且解析" />
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>


 
 

你可能感兴趣的:(tomcat,json,android,exception,String,layout)