Android中"get","post"請求的其中三種常用資料提交方式

來源:互聯網
上載者:User

標籤:

一.使用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);

  }

});

 

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

Android中"get","post"請求的其中三種常用資料提交方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.