方式一:HttpPost(import org.apache.http.client.methods.HttpPost)
private Button button1, button2, button3;
private TextView textView1;
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// URLַ
// String uriAPI =
// "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
String uriAPI = "http://172.20.0.206:8082//TestServelt/login.do";
/* 建立HTTP Post連線 */
HttpPost httpRequest = new HttpPost(uriAPI);
// Post運作傳送變數必須用NameValuePair[]陣列儲存
// 傳參數 服務端獲取的方法爲request.getParameter("name")
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", "this is post"));
try {
// 發出HTTP request
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 取得HTTP response
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
// 若狀態碼爲200 ok
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 取出迴應字串
String strResult = EntityUtils.toString(httpResponse.getEntity());
textView1.setText(strResult);
} else {
textView1.setText("Error Response" + httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
textView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
textView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
textView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
方式二:HttpURLConnection、URL(import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;)
private void httpUrlConnection() {
try {
String pathUrl = "http://172.20.0.206:8082/TestServelt/login.do";
// 建立連接
URL url = new URL(pathUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// //設置連接屬性
httpConn.setDoOutput(true);// 使用 URL 連接進行輸出
httpConn.setDoInput(true);// 使用 URL 連接進行輸入
httpConn.setUseCaches(false);// 忽略緩存
httpConn.setRequestMethod("POST");// 設置URL請求方法
String requestString = "客服端要以以流方式發送到服務端的數據...";
// 設置請求屬性
// 獲得數據字節數據,請求數據流的編碼,必須和下面服務器端處理請求流的編碼一致
byte[] requestStringBytes = requestString.getBytes(ENCODING_UTF_8);
httpConn.setRequestProperty("Content-length", "" + requestStringBytes.length);
httpConn.setRequestProperty("Content-Type", "application/octet-stream");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 維持長連接
httpConn.setRequestProperty("Charset", "UTF-8");
//
String name = URLEncoder.encode("黃武藝", "utf-8");
httpConn.setRequestProperty("NAME", name);
// 建立輸出流,並寫入數據
OutputStream outputStream = httpConn.getOutputStream();
outputStream.write(requestStringBytes);
outputStream.close();
// 獲得響應狀態
int responseCode = httpConn.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {// 連接成功
// 當正確響應時處理數據
StringBuffer sb = new StringBuffer();
String readLine;
BufferedReader responseReader;
// 處理響應流,必須與服務器響應流輸出的編碼一致
responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), ENCODING_UTF_8));
while ((readLine = responseReader.readLine()) != null) {
sb.append(readLine).append("\n");
}
responseReader.close();
tv.setText(sb.toString());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
補充:
標準的http post傳輸方式,以上面爲例子,如下:
requestStringBytes =“customerId=3461&nickName=黃&[email protected]&areaCode=0592&cityCode=350200®isterPhone=18906051120”服務端通過getParamer("customerId")獲取。
如果把參數值放在URL,例如http://172.20.0.206:8082/TestServelt/login.do?customerId=3461&nickName=黃&[email protected]&areaCode=0592&cityCode=350200®isterPhone=18906051120
雖然是以post方式傳輸,但是實際上還是以get方式提交