package com.zq.jsontest;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv1;
private TextView tv2;
private TextView tv3;
// private JSONArray jsonArray;
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case 0:
try {
JSONObject data1 = (JSONObject) msg.obj;
String id = data1.getString("id");
String name = data1.getString("name");
String version = data1.getString("version");
tv1.setText("id is: " + id);
tv2.setText("name is: " + name);
tv3.setText("version is: " + version);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendRequestWithHttpClient();
init();
}
private void init() {
// TODO Auto-generated method stub
tv1 = (TextView)findViewById(R.id.num);
tv2 = (TextView)findViewById(R.id.name);
tv3 = (TextView)findViewById(R.id.version);
}
private void sendRequestWithHttpClient() {
// TODO Auto-generated method stub
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://10.0.6.13/test/data.json");
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
parseJSONWithJSONObject(response);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
private void parseJSONWithJSONObject(String jsonData) {
// TODO Auto-generated method stub
/* for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
String num = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
Log.d("ZQ", "id is" + num);
Log.d("ZQ","name is" + name);
Log.d("ZQ", "version is" + version);
}
*/
try {
//创建JSON对象
JSONObject data = new JSONObject(jsonData);
Message message = new Message();
message.what = 0;
message.obj = data;
handler.sendMessage(message);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
handler中传递JSON数据的写法
最新推荐文章于 2024-09-20 20:01:20 发布