免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 894 | 回复: 0
打印 上一主题 下一主题

Android HttpClient网络通信 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览
  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 = "http://192.168.0.132:8080/Android/httpreq.jsp?par=request-get";
  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 = "http://192.168.0.132:8080/Android/httpreq.jsp";
  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 = "http://192.168.0.132:8080/Android/httpreq.jsp";
  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. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP