Android用WAMPSERVER HttpClient 实现Get请求

  1. package com.android;  
  2.   
  3. import java.io.IOException;  
  4. import org.apache.http.HttpResponse;  
  5. import org.apache.http.HttpStatus;  
  6. import org.apache.http.client.ClientProtocolException;  
  7. import org.apache.http.client.HttpClient;  
  8. import org.apache.http.client.methods.HttpGet;  
  9. import org.apache.http.impl.client.DefaultHttpClient;  
  10. import org.apache.http.util.EntityUtils;  
  11. import android.app.Activity;  
  12. import android.content.Intent;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.widget.Button;  
  16. import android.widget.TextView;  
  17.   
  18. public class Activity02 extends Activity {  
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.http);  
  24.         TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);  
  25.         // http地址  
  26.         String httpUrl = "http://192.168.1.9:8080/linelabel.php"; // 定义需要获取的内容来源地址
  27.         HttpGet httpRequest = new HttpGet(httpUrl);  
  28.         try {  
  29.             // 取得HttpClient对象  
  30.             HttpClient httpclient = new DefaultHttpClient();  
  31.             // 请求HttpClient,取得HttpResponse  
  32.             HttpResponse httpResponse = httpclient.execute(httpRequest);  
  33.             // 请求成功  
  34.             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  35.                 // 取得返回的字符串  
  36.                 String strResult = EntityUtils.toString(httpResponse  
  37.                         .getEntity());  
  38.                 mTextView.setText(strResult);  
  39.             } else {  
  40.                 mTextView.setText("请求错误!");  
  41.             }  
  42.         } catch (ClientProtocolException e) {  
  43.             mTextView.setText(e.getMessage().toString());  
  44.         } catch (IOException e) {  
  45.             mTextView.setText(e.getMessage().toString());  
  46.         } catch (Exception e) {  
  47.             mTextView.setText(e.getMessage().toString());  
  48.         }  
  49.   
  50.         // 设置按键事件监听  
  51.         Button button_Back = (Button) findViewById(R.id.Button_Back);  
  52.         /* 监听button的事件信息 */  
  53.         button_Back.setOnClickListener(new Button.OnClickListener() {  
  54.             public void onClick(View v) {  
  55.                 /* 新建一个Intent对象 */  
  56.                 Intent intent = new Intent();  
  57.                 /* 指定intent要启动的类 */  
  58.                 intent.setClass(Activity02.this, Activity01.class);  
  59.                 /* 启动一个新的Activity */  
  60.                 startActivity(intent);  
  61.                 /* 关闭当前的Activity */  
  62.                 Activity02.this.finish();  
  63.             }  
  64.         });  
  65.     }  
  66. }  

你可能感兴趣的:(httpclient)