Chinaunix首页 | 论坛 | 博客
  • 博客访问: 284688
  • 博文数量: 68
  • 博客积分: 1474
  • 博客等级: 上尉
  • 技术积分: 616
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-12 12:07
文章分类

全部博文(68)

文章存档

2011年(68)

分类: 嵌入式

2011-07-04 11:29:49

  1. package com.Aina.Android;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.UnsupportedEncodingException;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.util.ArrayList;
  11. import java.util.List;

  12. import org.apache.http.HttpEntity;
  13. import org.apache.http.HttpResponse;
  14. import org.apache.http.HttpStatus;
  15. import org.apache.http.NameValuePair;
  16. import org.apache.http.client.ClientProtocolException;
  17. import org.apache.http.client.HttpClient;
  18. import org.apache.http.client.entity.UrlEncodedFormEntity;
  19. import org.apache.http.client.methods.HttpGet;
  20. import org.apache.http.client.methods.HttpPost;
  21. import org.apache.http.impl.client.DefaultHttpClient;
  22. import org.apache.http.message.BasicNameValuePair;
  23. import org.apache.http.util.EntityUtils;

  24. import android.app.Activity;
  25. import android.os.Bundle;
  26. import android.os.Handler;
  27. import android.os.Message;
  28. import android.view.View;
  29. import android.widget.Button;
  30. import android.widget.TextView;

  31. public class Test extends Activity implements Runnable {
  32.     /** Called when the activity is first created. */
  33.     private Button btn_get = null;
  34.     private Button btn_post = null;
  35.     private TextView tv_rp = null;

  36.     @Override
  37.         public void onCreate(Bundle savedInstanceState) {
  38.             super.onCreate(savedInstanceState);
  39.             setContentView(R.layout.main);
  40.             btn_get = (Button) this.findViewById(R.id.Button01);
  41.             btn_post = (Button) this.findViewById(R.id.Button02);
  42.             tv_rp = (TextView) this.findViewById(R.id.TextView);
  43.             btn_get.setOnClickListener(new Button.OnClickListener() {

  44.                 public void onClick(View v) {
  45.                     // TODO Auto-generated method stub
  46.                     String httpUrl = "";
  47.                     HttpGet request = new HttpGet(httpUrl);
  48.                     HttpClient httpClient = new DefaultHttpClient();
  49.                     try {
  50.                         HttpResponse response = httpClient.execute(request);
  51.                         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  52.                             String str = EntityUtils.toString(response.getEntity());
  53.                             tv_rp.setText(str);
  54.                         } else {
  55.                             tv_rp.setText("请求错误");
  56.                         }
  57.                     } catch (ClientProtocolException e) {
  58.                         // TODO Auto-generated catch block
  59.                         e.printStackTrace();
  60.                     } catch (IOException e) {
  61.                         // TODO Auto-generated catch block
  62.                         e.printStackTrace();
  63.                     }
  64.                 }

  65.             });
  66.             btn_post.setOnClickListener(new Button.OnClickListener() {

  67.                 public void onClick(View v) {
  68.                     // TODO Auto-generated method stub
  69.                     String httpUrl = "";
  70.                     HttpPost request = new HttpPost(httpUrl);
  71.                     List<NameValuePair> params = new ArrayList<NameValuePair>();
  72.                     params.add(new BasicNameValuePair("par", "request-post"));
  73.                     try {
  74.                         HttpEntity entity = new UrlEncodedFormEntity(params,
  75.                             "UTF-8");
  76.                         request.setEntity(entity);
  77.                         HttpClient client = new DefaultHttpClient();
  78.                         HttpResponse response = client.execute(request);
  79.                         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  80.                             String str = EntityUtils.toString(response.getEntity());
  81.                             tv_rp.setText(str);
  82.                         } else {
  83.                             tv_rp.setText("请求错误");
  84.                         }
  85.                     } catch (UnsupportedEncodingException e) {
  86.                         // TODO Auto-generated catch block
  87.                         e.printStackTrace();
  88.                     } catch (ClientProtocolException e) {
  89.                         // TODO Auto-generated catch block
  90.                         e.printStackTrace();
  91.                     } catch (IOException e) {
  92.                         // TODO Auto-generated catch block
  93.                         e.printStackTrace();
  94.                     }
  95.                 }

  96.             });
  97.             new Thread(this).start();
  98.         }

  99.     public void refresh() {
  100.         String httpUrl = "";
  101.         try {
  102.             URL url = new URL(httpUrl);
  103.             HttpURLConnection urlConn = (HttpURLConnection) url
  104.                 .openConnection();
  105.             urlConn.connect();
  106.             InputStream input = urlConn.getInputStream();
  107.             InputStreamReader inputreader = new InputStreamReader(input);
  108.             BufferedReader reader = new BufferedReader(inputreader);
  109.             String str = null;
  110.             StringBuffer sb = new StringBuffer();
  111.             while ((str = reader.readLine()) != null) {
  112.                 sb.append(str).append("\n");
  113.             }
  114.             if (sb != null) {
  115.                 tv_rp.setText(sb.toString());
  116.             } else {
  117.                 tv_rp.setText("NULL");
  118.             }
  119.             reader.close();
  120.             inputreader.close();
  121.             input.close();
  122.             reader = null;
  123.             inputreader = null;
  124.             input = null;
  125.         } catch (MalformedURLException e) {
  126.             e.printStackTrace();
  127.         } catch (IOException e) {
  128.             // TODO Auto-generated catch block
  129.             e.printStackTrace();
  130.         }
  131.     }

  132.     public Handler handler = new Handler() {
  133.         public void handleMessage(Message msg) {
  134.             super.handleMessage(msg);
  135.             refresh();
  136.         }
  137.     };

  138.     public void run() {
  139.         // TODO Auto-generated method stub
  140.         while (true) {
  141.             try {
  142.                 Thread.sleep(1000);
  143.                 handler.sendMessage(handler.obtainMessage());
  144.             } catch (InterruptedException e) {
  145.                 // TODO Auto-generated catch block
  146.                 e.printStackTrace();
  147.             }
  148.         }
  149.     }
  150. }
阅读(1718) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

tianbianfei2011-07-14 11:01:55

楼主,请帮忙解决一个问题好吗?http://doumiw.com/market/community/t!showTopic.do?topicId=24