Android手机客户端通过JSP实现与Tomcat服务器端通信(Msql数据库,Json作为载体)--客户端代码

1.客户端文件,JsonParse,Json文件解析

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.jiangqq.bean.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();

	}
}

2.客户端主要类,MainActivity,发送请求


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

import com.jiangqq.bean.Person;
import com.jiangqq.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;
	private static final String urlPath = "http://192.168.1.100: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);//在手机上显示提示Toast,2秒
				Log.i(TAG, e.toString());//DDMS中显示提示

			}
		}
	}
}


3.同上一篇的,Person构造的数据类

package com.jiangqq.bean;

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
				+ "]";
	}
	
	

}


4.AndroidManifest.xml别忘记添加

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

<?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=".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>




你可能感兴趣的:(tomcat,json,jsp,数据库,android,手机)