Android的HTTP协议

1.超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。  2. java接口 --------java.net.*
3. apache 接口---------org.apache.http.*


Apache提供的HttpCient,实现起来简单方便:

A: GET方式操作

  1. public void get() {
  2.   String url = httpUrl + "?text1=" + text1.getText().toString()
  3.   + "&text2=" + text2.getText().toString();
  4.   // 创建HttpGet对象
  5.   HttpGet request = new HttpGet(url);
  6.   // 创建HttpClient对象
  7.   HttpClient client = new DefaultHttpClient();
  8.   HttpResponse httpResponse = null;
  9.   try {
  10.   httpResponse = client.execute(request);
  11.   if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  12.   text3.setText(EntityUtils.toString(httpResponse.getEntity(),
  13.   "utf-8"));
  14.   }
  15.   } catch (ClientProtocolException e) {
  16.   e.printStackTrace();
  17.   } catch (IOException e) {
  18.   e.printStackTrace();
  19.   }
  20.   }
  21.   public void get() {
  22.   String url = httpUrl + "?text1=" + text1.getText().toString()
  23.   + "&text2=" + text2.getText().toString();
  24.   // 创建HttpGet对象
  25.   HttpGet request = new HttpGet(url);
  26.   // 创建HttpClient对象
  27.   HttpClient client = new DefaultHttpClient();
  28.   HttpResponse httpResponse = null;
  29.   try {
  30.   httpResponse = client.execute(request);
  31.   if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  32.   text3.setText(EntityUtils.toString(httpResponse.getEntity(),
  33.   "utf-8"));
  34.   }
  35.   } catch (ClientProtocolException e) {
  36.   e.printStackTrace();
  37.   } catch (IOException e) {
  38.   e.printStackTrace();
  39.  }
  40.   }

B:POST方式操作

  1. public void post() {

  2.   // 创建HttpPost对象

  3.   HttpPost httpRequest = new HttpPost(httpUrl);

  4.   // 创建传递参数集合

  5.   List params = new ArrayList();

  6.   params.add(new BasicNameValuePair("text1", text1.getText().toString()));

  7.   params.add(new BasicNameValuePair("text2", text2.getText().toString()));

  8.   // 设置字符集

  9.   try {

  10.   HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");

  11.   httpRequest.setEntity(entity);

  12.   // 创建连接对象

  13.   HttpClient client = new DefaultHttpClient();

  14.   // 执行连接

  15.   HttpResponse response = client.execute(httpRequest);

  16.   if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

  17.   text3.setText(EntityUtils.toString(response.getEntity(),

  18.   "utf-8"));

  19.   }

  20.   } catch (UnsupportedEncodingException e) {

  21.   // TODO Auto-generated catch block

  22.   e.printStackTrace();

  23.   } catch (ClientProtocolException e) {

  24.   // TODO Auto-generated catch block

  25.   e.printStackTrace();

  26.   } catch (IOException e) {

  27.   // TODO Auto-generated catch block

  28.   e.printStackTrace();

  29.   }

  30.   }

  31.   public void post() {

  32.   // 创建HttpPost对象

  33.   HttpPost httpRequest = new HttpPost(httpUrl);

  34.   // 创建传递参数集合

  35.   List params = new ArrayList();

  36.   params.add(new BasicNameValuePair("text1", text1.getText().toString()));

  37.   params.add(new BasicNameValuePair("text2", text2.getText().toString()));

  38.   // 设置字符集

  39.   try {

  40.   HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");

  41.   httpRequest.setEntity(entity);

  42.   // 创建连接对象

  43.   HttpClient client = new DefaultHttpClient();
  44. // 执行连接
  45.   HttpResponse response = client.execute(httpRequest);
  46.   if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  47.   text3.setText(EntityUtils.toString(response.getEntity(),
  48.   "utf-8"));
  49.   }
  50.   } catch (UnsupportedEncodingException e) {
  51.   // TODO Auto-generated catch block
  52.   e.printStackTrace();
  53.   } catch (ClientProtocolException e) {
  54.   // TODO Auto-generated catch block
  55.   e.printStackTrace();
  56.   } catch (IOException e) {
  57.   // TODO Auto-generated catch block
  58.   e.printStackTrace();
  59.   }
  60.   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值