Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android中"get","post"請求的其中三種常用數據提交方式,android常用數據

Android中"get","post"請求的其中三種常用數據提交方式,android常用數據

編輯:關於android開發

Android中"get","post"請求的其中三種常用數據提交方式,android常用數據


一.使用HttpURLConnection提交數據

"get"請求

代碼:

String path = "http://地址?數據1名字=" + URLEncoder.encode(數據1,"utf-8") + "&數據2名字=" +URLEncoder.encode(數據2,"utf-8");

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

//這裡設置請求方式要寫為大寫

conn.setRequestMethod("GET");

conn.setConnectTimeout(5000);

int code = conn.getResponseCode();

if(code == 200){

  InputStream is = conn.getInputStream();

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int len = -1;
  byte[] buffer = new byte[1024];
  while ((len = is.read(buffer)) != -1) {
    baos.write(buffer, 0, len);
  }
  is.close();

  //這樣就得到服務器返回的數據了
  result = baos.toString();

}

 

 

"post"請求

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

//1這裡設置請求方式要寫為大寫

conn.setRequestMethod("POST");

//設置響應時長

conn.setConnectTimeout(5000);

//2設置http請求數據的類型為表單類型

conn.setRequestProperty("Content-type","application/x-www-form-urlencoded");

String data = "數據1名字=" +URLEncoder.encode(數據1,"utf-8") + "&數據2名字=" + URLEncoder.encode(數據2,"utf-8"); 

//3設置給服務器寫的數據的長度

conn.setRequestProperty("Content-Length",String.valueOf(data.length()));

//4指定要給服務器寫數據

conn.setDoOutput(true);

//5開始向服務器寫數據

conn.getOutputStream().write(data.getBytes);

int code = conn.getResponseCode();

if(code == 200){

  InputStream is = conn.getInputStream();

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int len = -1;
  byte[] buffer = new byte[1024];
  while ((len = is.read(buffer)) != -1) {
    baos.write(buffer, 0, len);
  }
  is.close();

  //注意:這裡回流的編碼默認是"utf-8"的

  result = baos.toString();

}

二.使用HttpClient提交數據

注:HttpClient會被內置到Android SDK中,可以不添加任何額外jar包直接使用,將文件從com文件夾復制粘貼到項目下就可以使用了

Get方式:

String path = "http://地址?數據1名字=" + URLEncoder.encode(數據1,"utf-8") + "&數據2名字" + URLEncoder.encode(數據2,"utf-8");

//可以將其過程理解為用戶浏覽器操作

//1打開浏覽器

HttpClient client = new DefaultHttpClient();

//2輸入地址

HttpGet httpGet = new HttpGet(path);

//3敲回車

HttpResponse response = client.execute(httpGet);

//獲取狀態碼

int code = response.getStatusLine().getStatusCode();

 

Post方式:

String path = "http://地址";

//1打開浏覽器

HttpClient client = new DefaultHttpClient();

//2輸入地址

HttpPost httpPost = new HttpPost(path);

List<NameValuePair> parameters = new ArrayList<NameValuePair>();

parameters.add(new BasicNameValuePair("數據1名字",數據1));

parameters.add(new BasicNameValuePair("數據2名字",數據1));

httpPost.setEntity(new UrlEncodedFormEntity(parameters,"utf-8"));

//3敲回車

HttpResponse response = client.execute(httpPost);

//4獲取狀態碼

int code = response.getStatusLine().getStatusCode();

 

三.使用AsyncHttpClient框架提交數據

該源碼可以在網上下載

將下載好的的源碼中src目錄中源碼拷貝到自己的工程的src目錄下

GET方式:

//請求路徑

String path = "http://地址?數據1名字=" + URLEncoder.encode(數據1) + "&數據2名字" + URLEncoder.encode(數據2);

AsyncHttpClient client = new AsyncHttpClient();

client.get(path,new AsyncHttpResponseHandler() {

  public void onSuccess(int statusCode,Header[]headers,byte[]responseBody){

  //請求成功

    new String(responseBody);//返回的數據

}

  public void onFailure(int statusCode,Header[]headers,byte[]responseBody,Throwable error) {

  //請求失敗

    String(responseBody);

  }

});

 

POST方式:

String path = "http://地址";

AsyncHttpClient client = new AsyncHttpClient();

RequestParams params = new RequestParams();

params.put("數據1名字",數據1);

params.put("數據2名字",數據2);

client.post(path,params,new AsyncHttpResponseHandler() {

  public void onSuccess(int statusCode,Header[]headers,byte[]responseBody){

  //請求成功

    new String(responseBody);//返回的數據

}

  public void onFailure(int statusCode,Header[]headers,byte[]responseBody,Throwable error) {

  //請求失敗

    String(responseBody);

  }

});

 

以上就是基本代碼了,寫的不好希望大家不要見怪,也希望和大家多多交流.

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