android http

开发android程序我们会遇到各种关于网络请求

所以我们开发的过程中,难免会用关于网络操作的

 java.net.*,又有org.apache.http.*,

在数据处理方面支持json,xml等常用的数据格式。

 

 JSON 的优势是数据轻便,小巧,

  XML 比较笨重,但是它的存在自然有他的道理

  apche.httpclient

  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 http)