首先要导包gson-2.2.4.jar到libs下
MainActivity.java
manifest.xml
MainActivity.java
package com.json.web_study.test_01;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button button;
private MyHandler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
handler = new MyHandler();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.button1) {
MyThread thread = new MyThread();
thread.start();
}
}
public String readData(InputStream inSream, String charsetName)
throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inSream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inSream.close();
return new String(data, charsetName);
}
class MyThread extends Thread {
@Override
public void run() {
try {
URL url = new URL(
"http://192.168.1.106:8080/MyWeb_Json/myjson2.json");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
InputStream input = conn.getInputStream();
String json = readData(input, "utf-8");
// 普通解析
// JSONArray jlist=new JSONArray(Test.getJson());
//
// for (int i = 0; i < jlist.length(); i++) {
// JSONObject jobj=jlist.getJSONObject(i);
// String name=jobj.getString("name");
// String address=jobj.getString("address");
//
// System.out.println(name+"---------"+address);
//
// Message message=new Message();
// Bundle data=new Bundle();
// data.putString("name", name);
// data.putString("address", address);
// message.setData(data);
//
// handler.sendMessage(message);
//
// sleep(1000);
//
// }
// gson 解析 (导包)
Gson gson = new Gson();
Type listType = new TypeToken<List<Person>>() {
}.getType();
List<Person> persons = gson.fromJson(json, listType);
for (int i = 0; i < persons.size(); i++) {
System.out.println(persons.get(i).getName() + "---------"
+ persons.get(i).getAddress());
Message message = new Message();
Bundle data = new Bundle();
data.putString("name", persons.get(i).getName());
data.putString("address", persons.get(i).getAddress());
message.setData(data);
handler.sendMessage(message);
sleep(1000);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Bundle data = msg.getData();
String name = data.getString("name");
String address = data.getString("address");
Toast.makeText(MainActivity.this, name + "---------" + address,
Toast.LENGTH_SHORT).show();
}
}
}
manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>