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.   }

你可能感兴趣的:(android)