package com.android.myjson;
import java.util.List;
import java.util.Map;
import com.android.myjson.domain.Person;
import com.android.myjson.http.HttpUtils;
import com.android.myjson.json.JsonTools;
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.Toast;
public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button person, persons, liststring, listmap;
private static final String TAG = "Main";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
person = (Button) this.findViewById(R.id.person);
persons = (Button) this.findViewById(R.id.persons);
liststring = (Button) this.findViewById(R.id.liststring);
listmap = (Button) this.findViewById(R.id.listmap);
person.setOnClickListener(this);
persons.setOnClickListener(this);
liststring.setOnClickListener(this);
listmap.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.person:
String path = "http://192.168.1.127:8080/json_service/servlet/JsonAction?action_flag=person";
String jsonString = HttpUtils.getJsonContent(path);
Person person = JsonTools.getPerson("person", jsonString);
System.out.println("====android客户端解析的数据的内容====="+person);
Log.i(TAG, person.toString());
break;
case R.id.persons:
String path2 = "http://192.168.1.127:8080/json_service/servlet/JsonAction?action_flag=persons";
String jsonString2 = HttpUtils.getJsonContent(path2);
List<Person> list2 = JsonTools.getPersons("persons", jsonString2);
System.out.println("====android客户端解析的数据的内容====="+list2);
Log.i(TAG, list2.toString());
break;
case R.id.liststring:
String path3 = "http://192.168.1.127:8080/json_service/servlet/JsonAction?action_flag=liststring";
String jsonString3 = HttpUtils.getJsonContent(path3);
List<String> list3 = JsonTools.getList("liststring", jsonString3);
Log.i(TAG, list3.toString());
break;
case R.id.listmap:
String path4 = "http://192.168.1.127:8080/json_service/servlet/JsonAction?action_flag=listmap";
String jsonString4 = HttpUtils.getJsonContent(path4);
List<Map<String, Object>> list4 = JsonTools.listKeyMaps("listmap",
jsonString4);
Log.i(TAG, list4.toString());
break;
}
}
}
|
package com.android.myjson.domain;
public class Person {
private int id;
private String name;
public Person(
int id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
private String address;
public int getId() {
return id;
}
@Override
public String toString() {
return "Person [address=" + address + ", id=" + id + ", name=" + name
+ "]";
}
public void setId(
int id) {
this.id = id;
}
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 Person() {
//
TODO Auto-generated constructor stub
}
}
|
package com.android.myjson.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtils {
public HttpUtils() {
// TODO Auto-generated constructor stub
}
public static String getJsonContent(String url_path) {
try {
URL url = new URL(url_path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
int code = connection.getResponseCode();
if (code == 200) {
return changeInputStream(connection.getInputStream());
}
} catch (Exception e) {
// TODO: handle exception
}
return "";
}
private static String changeInputStream(InputStream inputStream) {
// TODO Auto-generated method stub
String jsonString = "";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = 0;
byte[] data = new byte[1024];
try {
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
jsonString = new String(outputStream.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonString;
}
}
|
package com.android.myjson.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.android.myjson.domain.Person;
/**
* 完成对json数据的解析
*
* @author jack
*
*/
public class JsonTools {
public JsonTools() {
// TODO Auto-generated constructor stub
}
public static Person getPerson(String key, String jsonString) {
Person person = new Person();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject personObject = jsonObject.getJSONObject("person");
person.setId(personObject.getInt("id"));
person.setName(personObject.getString("name"));
person.setAddress(personObject.getString("address"));
} catch (Exception e) {
// TODO: handle exception
}
return person;
}
public static List<Person> getPersons(String key, String jsonString) {
List<Person> list = new ArrayList<Person>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
// 返回json的数组
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Person person = new Person();
person.setId(jsonObject2.getInt("id"));
person.setName(jsonObject2.getString("name"));
person.setAddress(jsonObject2.getString("address"));
list.add(person);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
public static List<String> getList(String key, String jsonString) {
List<String> list = new ArrayList<String>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
String msg = jsonArray.getString(i);
list.add(msg);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
public static List<Map<String, Object>> listKeyMaps(String key,
String jsonString) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> iterator = jsonObject2.keys();
while (iterator.hasNext()) {
String json_key = iterator.next();
Object json_value = jsonObject2.get(json_key);
if (json_value == null) {
json_value = "";
}
map.put(json_key, json_value);
}
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
}
|