日期:2014-05-16  浏览次数:20352 次

http请求返回并解析json数据

/*   注意get和post请求的发送区别
 * 1、创建HttpGet(或HttpPost)对象,将要请求的URL通过构造方法传入HttpGet(或HttpPost)对象中;
   2、使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST 请求,并返回HttpResponse对象;
   3、通过HttpResponse接口的getEntity方法返回响应信息。
 * */

public class TestHttpActivity extends Activity {
  private Button btn_get;
  private Button btn_post;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn_get=(Button)findViewById(R.id.btn_get);
        btn_post=(Button)findViewById(R.id.btn_post);
        btn_get.setOnClickListener(listener);
        btn_post.setOnClickListener(listener);
    }
    private OnClickListener listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_post:
				Log.i("TestHttpActivity", "ok");
				 DefaultHttpClient client = new DefaultHttpClient(); 
				 /**NameValuePair是传送给服务器的请求参数    param.get("name") **/
                 List<NameValuePair> list = new ArrayList<NameValuePair>();  
                 NameValuePair pair1 = new BasicNameValuePair("name", "name0001");  
                 NameValuePair pair2 = new BasicNameValuePair("age", "age0001");  
                 list.add(pair1);  
                 list.add(pair2);  
                 UrlEncodedFormEntity entity=null;
				try {
					/**设置编码 **/
					entity = new UrlEncodedFormEntity(list,"UTF-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
				/**新建一个post请求**/
                 HttpPost post = new HttpPost("http://aaron-pc.wsd.com/Test/testHttp");  
                 post.setEntity(entity);  
                 HttpResponse response=null;
                 String strResult="";
				try {
					/**客服端向服务器发送请求**/
					response = client.execute(post);
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
                   /**请求发送成功,并得到响应**/
                 if(response.getStatusLine().getStatusCode()==200){  
					try {
						  /**读取服务器返回过来的json字符串数据**/
						   strResult = EntityUtils.toString(response.getEntity());   
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
			
					 JSONObject jsonObject = null;
					try {
						/**把json字符串转换成json对象**/
						jsonObject = getJSON(strResult);
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					 String names="";
					
                     try {
                    	 /**
                    	  * jsonObject.getString("code") 取出code
                    	  * 比如这里返回的json 字符串为 [code:0,msg:"ok",data:[list:{"name":1},{"name":2}]]
                    	  * **/
                    	 
                    	 /**得到data这个key**/
                    	 String data=jsonObject.getString("data");
                    	 /**把data下的数据转换成json对象**/
                    	 JSONObject jDat = new JSONObject(data);
                    	 /**判断data对象下的list是否存在**/
                    	 if(jDat.get("list")!=null){
                    		 /**把list转换成jsonArray对象**/
                    		 JSONArray jarr = jDat.getJSONArray("list");
                    		 /**循环list对象**/
                    	 for (int i = 0; i < jarr.length(); i++) {
                    		 
                    		 /** **/
                    		 JSONObject jsono = (JSONObject) jarr.get(i); 
                    		 
                    		 /**取出list下的name的值 **/
                    		 names=names+jsono.getString("name");
                    		 
         				}
                    	 }
						Toast.makeText(TestHttpActivity.th