HttpClient请求数据(GET)并用JSON解析数据(一)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/id_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
       <TextView
        android:id="@+id/id_text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
          <TextView
        android:id="@+id/id_text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
             <TextView
        android:id="@+id/id_text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
	<Button 
	    android:id="@+id/id_btn"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="请求数据"
	    android:onClick="onClick"/>
    
</LinearLayout>
</ScrollView>

package com.tdxx.zhttp;
/**
 * {"RESULT_MSG":"调用接口成功!","RESULT_CODE":"1","RESULT_MAP":{"RESULT_LIST":
 * [{"ORDER_ID":2597,"ORDER_NO":"E0000014451","EXPECTED_DT":"2015-01-23 20:00:00","EXPECTED_DT2":"01-23 20:00","EXPECTED_DT4":"20:00","EXPECTED_DT3":"2015-01-23","PEOPLE_CNT":1,"USER_NM":"痛苦女士","PIC_ADDR":null,"ACCESS_NUMBER":"189***18188","NEW_ACCESS_NUMBER":"18912918188","USER_ID":"13382081268","TABLE_NM":"大厅","TABLE_ID":315,"STATUS":2,"CHANNEL_ID":"4159157725194425002","BD_USER_ID":"765475812613929230","PUSH_NUM":null,"B_USER_ID":2075,"CONFIRM_DT":1421984991000,"ORDER_STATE":2},
 * {"ORDER_ID":2556,"ORDER_NO":"E0000014416","EXPECTED_DT":"2015-01-16 17:30:00","EXPECTED_DT2":"01-16 17:30","EXPECTED_DT4":"17:30","EXPECTED_DT3":"2015-01-16","PEOPLE_CNT":1,"USER_NM":"程先生","PIC_ADDR":"http://182.254.155.223/HEAD_PIC/1748_20150110113744245.jpg","ACCESS_NUMBER":"133***81268","NEW_ACCESS_NUMBER":"13382081268","USER_ID":"13382081268","TABLE_NM":"大厅","TABLE_ID":315,"STATUS":5,"CHANNEL_ID":"","BD_USER_ID":"","PUSH_NUM":null,"B_USER_ID":1748,"CONFIRM_DT":1421398453000,"ORDER_STATE":2},
 * {"ORDER_ID":2555,"ORDER_NO":"E0000014415","EXPECTED_DT":"2015-01-16 17:30:00","EXPECTED_DT2":"01-16 17:30","EXPECTED_DT4":"17:30","EXPECTED_DT3":"2015-01-16","PEOPLE_CNT":1,"USER_NM":"程先生","PIC_ADDR":"http://182.254.155.223/HEAD_PIC/1748_20150110113744245.jpg","ACCESS_NUMBER":"133***81268","NEW_ACCESS_NUMBER":"13382081268","USER_ID":"13382081268","TABLE_NM":"大厅","TABLE_ID":315,"STATUS":5,"CHANNEL_ID":"","BD_USER_ID":"","PUSH_NUM":null,"B_USER_ID":1748,"CONFIRM_DT":1421398360000,"ORDER_STATE":2},
 * {"ORDER_ID":2494,"ORDER_NO":"E0000014372","EXPECTED_DT":"2015-01-15 21:30:00","EXPECTED_DT2":"01-15 21:30","EXPECTED_DT4":"21:30","EXPECTED_DT3":"2015-01-15","PEOPLE_CNT":1,"USER_NM":"程先生","PIC_ADDR":"http://182.254.155.223/HEAD_PIC/1748_20150110113744245.jpg","ACCESS_NUMBER":"133***81268","NEW_ACCESS_NUMBER":"13382081268","USER_ID":"13382081268","TABLE_NM":"大厅","TABLE_ID":315,"STATUS":2,"CHANNEL_ID":"","BD_USER_ID":"","PUSH_NUM":null,"B_USER_ID":1748,"CONFIRM_DT":1421312542000,"ORDER_STATE":2}]}}
 */
import java.io.IOException;
import java.util.ArrayList;
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.view.View;
import android.widget.TextView;

public class MainActivity extends Activity{
	private TextView textView1,textView2,textView3,textView4;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView1 = (TextView) findViewById(R.id.id_text1);
		textView2= (TextView) findViewById(R.id.id_text2);
		textView3 = (TextView) findViewById(R.id.id_text3);
		textView4 = (TextView) findViewById(R.id.id_text4);
	}


	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
//				holder =  (Holder) msg.obj;
//				textView.setText(holder.RESULT_MSG+holder.RESULT_CODE+"@@@@@"+holder.CONFIRM_DT+"@@@@"+holder.ORDER_ID);
					list = (ArrayList<Holder>) msg.obj;
					textView1.setText(list.get(0).ORDER_ID);
					textView2.setText(list.get(1).ORDER_ID);
					textView3.setText(list.get(2).ORDER_ID);
					textView4.setText(list.get(3).ORDER_ID);
					break;
			
			default:
				break;
			}
		}
	};

	Holder holder;
	ArrayList<Holder> list;
	private void sendRequestWithHttpClient() {
		new Thread(new Runnable(){
			@Override
			public void run() {
				try {
					String url = "http://192.168.1.1/HYMS_INTF/InteMgrController/excuteInteMgrResult.do?reqStr=%7B%22inteId%22%3A%227%22%2C%22expectedDt%22%3A%22%22%2C%22customerId%22%3A%2213382081268%22%2C%22tableTpId%22%3A%22%22%2C%22status%22%3A%22%22%2C%22pageRow%22%3A%2210%22%2C%22pageNum%22%3A%221%22%7D";
					<span style="color:#ff0000;"><strong>HttpClient httpClient = new DefaultHttpClient();//创建HttpClient对象
					HttpGet httpGet = new HttpGet(url);//创建HttpGet对象
					HttpResponse httpResponse =httpClient.execute(httpGet);
					if(httpResponse.getStatusLine().getStatusCode()==200){
						//请求和响应都成功了
						HttpEntity entity =httpResponse.getEntity();
						String response = EntityUtils.toString(entity,"utf-8");
						JSONObject jsonObj = new JSONObject(response);//把String对象转为JSONObject对象
						holder = new Holder();
						holder.RESULT_MSG = jsonObj.optString("RESULT_MSG");
						holder.RESULT_CODE=jsonObj.optString("RESULT_CODE");
						holder.RESULT_MAP =jsonObj.optString("RESULT_MAP");
						JSONObject jsonObj1 = new JSONObject(holder.RESULT_MAP);
						
						//下面就说JSONArray这里只解析2个数据
						JSONArray jsonArray =jsonObj1.optJSONArray("RESULT_LIST");//找个地方要注意
						list = new ArrayList<Holder>();
						for(int i=0;i<jsonArray.length();i++){
							holder = new Holder();
							JSONObject objj = jsonArray.optJSONObject(i);
							holder.ORDER_ID = objj.optString("ORDER_ID");
							holder.CONFIRM_DT = objj.optString("CONFIRM_DT");
							list.add(holder);
						}</strong>
</span>						Message msg = new Message();
						msg.what=1;
						msg.obj =list;
						handler.sendMessage(msg);
						/*JSONObject obje = jsonArray.optJSONObject(0);//这里只拿出下标为0的进行解析
						holder.ORDER_ID = obje.optString("ORDER_ID");
						holder.CONFIRM_DT = obje.optString("CONFIRM_DT");
						Message msg = new Message();
						msg.what=1;
						msg.obj =holder;
						handler.sendMessage(msg);*/
						
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
		
	}


	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.id_btn:
			sendRequestWithHttpClient();
			break;

		default:
			break;
		}
	}

}
//建holder对象用来直接传对象
class  Holder{
	String RESULT_MSG;
	String RESULT_CODE;
	String RESULT_MAP;
	String RESULT_LIST;
	String ORDER_ID;
	String CONFIRM_DT;
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值