记一个JSON解析,客户端,服务端

安卓端


package com.example.httprequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private Button get, post;
	private TextView info;
	private Handler handle = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		get = (Button) findViewById(R.id.get);
		post = (Button) findViewById(R.id.post);
		info = (TextView) findViewById(R.id.info);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		get.setOnClickListener(this);
		post.setOnClickListener(this);

	}

	private List<MyJosn> list=new ArrayList<MyJosn>();			//用来存数据
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.get:
			//使用网络操作放在线程中
			new Thread(new Runnable() {

				@Override
				public void run() {
					final String result = doGet();
//{key,[{key1,value,key2,value,key3,vaule},{key1,value,key2,value,key3,vaule},{key1,value,key2,value,key3,vaule}]}
					try {
						JSONObject jo=new JSONObject(result);		//获得jsonobject
						JSONArray ja=jo.getJSONArray("data");		//取得数组
						for(int i=0;i<ja.length();i++){				//循环赋值将数据存到list中;
							JSONObject joo=ja.getJSONObject(i);
							
							String key1=joo.getString("key1");
							String key2=joo.getString("key2");
							String key3=joo.getString("key3");
							MyJosn my=new MyJosn();					//将数据存到自定义类中
							my.setKey1(key1);
							my.setKey2(key2);
							my.setKey3(key3);
							list.add(my);
						}
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//更新ui
					handle.post(new Runnable() {

						@Override
						public void run() {
							info.setText(list.toString());

						}
					});
				}
			}).start();

			break;
		case R.id.post:												//post的请求方式,没有解析返回的json
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						final String result=doPost();
						
						handle.post(new Runnable() {
							
							@Override
							public void run() {
								info.setText(result);
								
							}
						});
						
					}
				}).start();
			break;
		default:
			break;
		}

	}

	private String doGet() {
		String result = null;

		try {
			HttpClient client = new DefaultHttpClient();						//默认http客户端
			HttpGet request = new HttpGet(
					"http://10.10.9.3:80/108Project/first?name=aaa");			//实例化一个httpGet地址后面附加参数
			HttpResponse response = client.execute(request);					//执行request,获得一个响应,阻塞操作

			result = EntityUtils.toString(response.getEntity());				//取得数据,用entityutil工具转换成String;

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;															//返回一个字符串
	}

	private String doPost() {
		String result = null;

		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost request = new HttpPost(									//1。post请求
					"http://10.10.9.3:80/108Project/first");
			
			List<NameValuePair> list=new ArrayList<NameValuePair>();			//4.实例化一个
			
			list.add(new BasicNameValuePair("name", "bbb"));					//5.添加	
	
			HttpEntity entity=new UrlEncodedFormEntity(list, "UTF-8");			//3.那么久new一个httpentity实现类吧,然后发现需要一个List<NameValuePair>,那在弄一个吧

			request.setEntity(entity);											//2.设置请求内容,要用到一个httpentity;

			HttpResponse response = client.execute(request);					//6.发送请求等待响应

			result = EntityUtils.toString(response.getEntity());				//7.取得结果

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值