Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發——HTTP通訊

Android開發——HTTP通訊

編輯:Android開發實例

說Android是一款互聯網手機操作系統一點也不過為過。打開G2的"大抽屜”,一大堆的軟件,大部分都是需要網絡支持才能正常運行的。

曾經和同學說:沒有網絡支持,G2跟Nokia 1200沒啥區別!

既然Android定位為“網絡操作系統”,自然提供了很威水的網絡訪問接口。既有java.net.*,又有org.apache.http.*,在數據處理方面支持json,xml等常用的數據格式。

示例代碼如下:

   1:  
   2: public class HttpDemo extends Activity {
   3:  
   4:     private static final int MSGWHAT = 0x000000;
   5:  
   6:     private TextView textView;
   7:     private Button btnLink;
   8:     private EditText etUrl;
   9:     private String data;
  10:     /**
  11:      * 消息控制器,用來更新界面,因為在普通線程是無法用來更新界面的
  12:      */
  13:     private Handler handler = new Handler() {
  14:         @Override
  15:         public void handleMessage(Message msg) {
  16:             switch (msg.what) {
  17:             case MSGWHAT:
  18:                 //設置顯示文本
  19:                 textView.setText(data);
  20:                 break;
  21:             default:
  22:                 break;
  23:             }
  24:         }
  25:     };
  26:  
  27:     /** Called when the activity is first created. */
  28:     @Override
  29:     public void onCreate(Bundle savedInstanceState) {
  30:         super.onCreate(savedInstanceState);
  31:         setContentView(R.layout.main);
  32:         //注意界面控件的初始化的位置,不要放在setContentView()前面
  33:         initComponent();
  34:         btnLink.setOnClickListener(new OnClickListener() {
  35:  
  36:             @Override
  37:             public void onClick(View v) {
  38:                 //可以放在另外的線程完成
  39:                 try {
  40:                     data = getData();
  41:                 } catch (ClientProtocolException e) {
  42:                     data = e.getMessage();
  43:                 } catch (IOException e) {
  44:                     data = e.getMessage();
  45:                 } catch (URISyntaxException e) {
  46:                     data = e.getMessage();
  47:                 }
  48:                 Message msg = new Message();
  49:                 msg.what = MSGWHAT;
  50:                 handler.sendMessage(msg);
  51:             }
  52:         });
  53:     }
  54:  
  55:     /**
  56:      * 初始化界面組件
  57:      */
  58:     private void initComponent() {
  59:         textView = (TextView) findViewById(R.id.text);
  60:         btnLink = (Button) findViewById(R.id.btn_link);
  61:         etUrl = (EditText) findViewById(R.id.url);
  62:     }
  63:  
  64:     /**
  65:      * 取數據
  66:      * @return
  67:      * @throws ClientProtocolException
  68:      * @throws IOException
  69:      * @throws URISyntaxException
  70:      */
  71:     private String getData() throws ClientProtocolException, IOException, URISyntaxException {
  72:         String urlString = etUrl.getText().toString();
  73:         return request(urlString);
  74:     }
  75:  
  76:     /**
  77:      * 發送請求
  78:      * @param url 請求地址
  79:      * @return
  80:      * @throws ClientProtocolException
  81:      * @throws IOException
  82:      * @throws URISyntaxException
  83:      */
  84:     private String request(String url) throws ClientProtocolException, IOException, URISyntaxException {
  85:         HttpClient httpClient = new DefaultHttpClient();
  86:         URI uri = URIUtils.createURI("http", url, -1, null, null, null);
  87:         HttpGet get = new HttpGet(uri);
  88:         ResponseHandler<String> responseHandler = new BasicResponseHandler();
  89:         return httpClient.execute(get, responseHandler);
  90:     }
  91:     
  92:     @Override
  93:     protected void onDestroy() {
  94:         super.onDestroy();
  95:         System.gc();
  96:         System.exit(0);
  97:     }
  98: }

轉自:http://www.cnblogs.com/halzhang/archive/2010/05/28/1746591.html

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved