編輯:關於 Android 開發
一、使用 HttpURLConnection 提交資料
"GET" 請求:
String path = "https://地址?數據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 = "https://地址?數據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 = "https://地址";
//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 = "https://地址?數據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 = "https://地址";
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);
}
});
以上就是三種基本寫法,寫得不好還請多多包涵,也歡迎大家一起交流。
CircleImageView 與 CardView 搭配實作:圓形頭像加卡片列表
做出一張有質感的卡片列表 圓形頭像在現代 App 中幾乎是標配,配上 CardView 的陰影與圓角,一個簡單的列表就能有相當不錯的視覺層次。本文示範如何把這兩者組進 D
自定義 ListView Item 背景:selector 兩種實現方式
自定義 ListView Item 背景:selector 兩種實現方式 ListView 每一行的背景需要根據「按下」「選中」「獲得焦點」等狀態變化時,用 select
Volley 源碼深度解析:從使用到擴充的完整剖析
Volley 源碼深度解析 很早之前就想寫一篇關於 Volley 的源碼解析。最初學 Android 網路訪問時用的都是 HttpClient,剛接觸 Volley 便愛
Kotlin 擴展函數:擴充 Android 框架(KAD 08)
作者:Antonio Leiva 時間:Jan 11, 2017 原文連結:https://antonioleiva.com/extension-functions-ko