記一個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;
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章