Android的http兩種請求方式
http的兩種請求方式:POST和GET
由於Android的SDK包含org.apache.http包,所以不用導入jar了
GET方式:
String serverURL = "http://www.wwaattssuunn.com/127.0.0.1/xxx/xx.jsp?username=abc;
HttpGet httpRequest = new HttpGet(serverURL);// 建立http get聯機
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 發出http請求
if (httpResponse.getStatusLine().getStatusCode() == 200)
String result = EntityUtils.toString(httpResponse.getEntity());// 獲取相應的字符串
POST方式:
復制代碼
String uriAPI = "http://www.wwaattssuunn.com/127.0.0.1/xxx/xx.jsp"; //聲明網址字符串
HttpPost httpRequest = new HttpPost(uriAPI); //建立HTTP POST聯機
List <NameValuePair> params = new ArrayList <NameValuePair>(); //Post運作傳送變量必須用NameValuePair[]數組儲存
params.add(new BasicNameValuePair("str", "I am Post String"));
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); //發出http請求
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); //取得http響應
if(httpResponse.getStatusLine().getStatusCode() == 200)
String strResult = EntityUtils.toString(httpResponse.getEntity()); //獲取字符串
復制代碼