Android應用經常會和伺服器端交互,這就需要手機用戶端發送網路請求,下麵介紹四種常用網路請求方式,

java.net包中的HttpURLConnection類




Get方式:




view plain

 

// Get方式請求

 

public static void requestByGet() throws Exception {

 

String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";

 

// 新建一個URL物件

 

URL url = new URL(path);

 

// 打開一個HttpURLConnection連接

 

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

 

// 設置連接逾時時間

 

urlConn.setConnectTimeout(5 * 1000);

 

// 開始連接

 

urlConn.connect();

 

// 判斷請求是否成功

 

if (urlConn.getResponseCode() == HTTP_200) {

 

// 獲取返回的資料

 

byte[] data = readStream(urlConn.getInputStream());

 

Log.i(TAG_GET, "Get方式請求成功,返回資料如下:");

 

Log.i(TAG_GET, new String(data, "UTF-8"));

 

} else {

 

Log.i(TAG_GET, "Get方式請求失敗");

 

}

 

// 關閉連接

 

urlConn.disconnect();

 

}




Post方式:




view plain

 

// Post方式請求

 

public static void requestByPost() throws Throwable {

 

String path = "https://reg.163.com/logins.jsp";

 

// 請求的參數轉換為byte陣列

 

String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")

 

+ "&pwd=" + URLEncoder.encode("android", "UTF-8");

 

byte[] postData = params.getBytes();

 

// 新建一個URL物件

 

URL url = new URL(path);

 

// 打開一個HttpURLConnection連接

 

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

 

// 設置連接逾時時間

 

urlConn.setConnectTimeout(5 * 1000);

 

// Post請求必須設置允許輸出

 

urlConn.setDoOutput(true);

 

// Post請求不能使用緩存

 

urlConn.setUseCaches(false);

 

// 設置為Post請求

 

urlConn.setRequestMethod("POST");

 

urlConn.setInstanceFollowRedirects(true);

 

// 配置請求Content-Type

 

urlConn.setRequestProperty("Content-Type",

 

"application/x-www-form-urlencode");

 

// 開始連接

 

urlConn.connect();

 

// 發送請求參數

 

DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());

 

dos.write(postData);

 

dos.flush();

 

dos.close();

 

// 判斷請求是否成功

 

if (urlConn.getResponseCode() == HTTP_200) {

 

// 獲取返回的資料

 

byte[] data = readStream(urlConn.getInputStream());

 

Log.i(TAG_POST, "Post請求方式成功,返回資料如下:");

 

Log.i(TAG_POST, new String(data, "UTF-8"));

 

} else {

 

Log.i(TAG_POST, "Post方式請求失敗");

 

}

 

}







org.apache.http包中的HttpGet和HttpPost類






Get方式:




view plain

 

// HttpGet方式請求

 

public static void requestByHttpGet() throws Exception {

 

String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";

 

// 新建HttpGet物件

 

HttpGet httpGet = new HttpGet(path);

 

// 獲取HttpClient物件

 

HttpClient httpClient = new DefaultHttpClient();

 

// 獲取HttpResponse實例

 

HttpResponse httpResp = httpClient.execute(httpGet);

 

// 判斷是夠請求成功

 

if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {

 

// 獲取返回的資料

 

String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");

 

Log.i(TAG_HTTPGET, "HttpGet方式請求成功,返回資料如下:");

 

Log.i(TAG_HTTPGET, result);

 

} else {

 

Log.i(TAG_HTTPGET, "HttpGet方式請求失敗");

 

}

 

}




Post方式:




view plain

 

// HttpPost方式請求

 

public static void requestByHttpPost() throws Exception {

 

String path = "https://reg.163.com/logins.jsp";

 

// 新建HttpPost物件

 

HttpPost httpPost = new HttpPost(path);

 

// Post參數

 

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

 

params.add(new BasicNameValuePair("id", "helloworld"));

 

params.add(new BasicNameValuePair("pwd", "android"));

 

// 設置字元集

 

HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);

 

// 設置參數實體

 

httpPost.setEntity(entity);

 

// 獲取HttpClient物件

 

HttpClient httpClient = new DefaultHttpClient();

 

// 獲取HttpResponse實例

 

HttpResponse httpResp = httpClient.execute(httpPost);

 

// 判斷是夠請求成功

 

if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {

 

// 獲取返回的資料

 

String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");

 

Log.i(TAG_HTTPGET, "HttpPost方式請求成功,返回資料如下:");

 

Log.i(TAG_HTTPGET, result);

 

} else {

 

Log.i(TAG_HTTPGET, "HttpPost方式請求失敗");

 

}

 

}
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()