Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android 的HTTP協議

Android 的HTTP協議

編輯:高級開發

 1.超文本傳輸協議(HTTP,HyperText Transfer Protocol)是互聯網上應用最為廣泛的一種網絡協議。所有的WWW文件都必須遵守這個標准。設計HTTP最初的目的是為了提供一種發布和接收Html頁面的方法

  2. java接口 --------Java.Net.*

  3. apache 接口---------org.apache.http.*

  apache提供的HttpCIEnt,實現起來簡單方便:

  A: GET方式操作

  s

  Java代碼

  public void get() {

  String url = httpUrl + "?text1=" + text1.getText().toString()

  + "&text2=" + text2.getText().toString();

  // 創建HttpGet對象

  HttpGet request = new HttpGet(url);

  // 創建HttpClIEnt對象

  HttpClient client = new DefaultHttpClIEnt();

  HttpResponse httpResponse = null;

  try {

  httpResponse = clIEnt.execute(request);

  if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

  text3.setText(EntityUtils.toString(httpResponse.getEntity(),

  "utf-8"));

  }

  } catch (ClIEntProtocolException e) {

  e.printStackTrace();

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  public void get() {

  String url = httpUrl + "?text1=" + text1.getText().toString()

  + "&text2=" + text2.getText().toString();

  // 創建HttpGet對象

  HttpGet request = new HttpGet(url);

  // 創建HttpClIEnt對象

  HttpClient client = new DefaultHttpClIEnt();

  HttpResponse httpResponse = null;

  try {

  httpResponse = clIEnt.execute(request);

  if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

  text3.setText(EntityUtils.toString(httpResponse.getEntity(),

  "utf-8"));

  }

  } catch (ClIEntProtocolException e) {

  e.printStackTrace();

  } catch (IOException e) {

  e.printStackTrace();

  接上頁

  }

  }

  B:POST方式操作

  Java代碼

  public void post() {

  // 創建HttpPost對象

  HttpPost httpRequest = new HttpPost(httpUrl);

  // 創建傳遞參數集合

  List params = new ArrayList();

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

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

  // 設置字符集

  try {

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

  httpRequest.setEntity(entity);

  // 創建連接對象

  HttpClient client = new DefaultHttpClIEnt();

  // 執行連接

  HttpResponse response = clIEnt.execute(httpRequest);

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

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

  "utf-8"));

  }

  } catch (UnsupportedEncodingException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (ClIEntProtocolException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  public void post() {

  // 創建HttpPost對象

  HttpPost httpRequest = new HttpPost(httpUrl);

  // 創建傳遞參數集合

  List params = new ArrayList();

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

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

  // 設置字符集

  try {

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

  httpRequest.setEntity(entity);

  // 創建連接對象

  HttpClient client = new DefaultHttpClIEnt();

  接上頁

  // 執行連接

  HttpResponse response = clIEnt.execute(httpRequest);

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

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

  "utf-8"));

  }

  } catch (UnsupportedEncodingException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (ClIEntProtocolException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved