最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開(kāi)發(fā)中的幾種網(wǎng)絡(luò)請(qǐng)求方式詳解

 更新時(shí)間:2016年11月09日 16:47:43   作者:Snail  
本篇文章主要包括Android中的幾種網(wǎng)絡(luò)請(qǐng)求方式詳解,具有一定的參考價(jià)值,有需要的可以了解一下。

Android應(yīng)用經(jīng)常會(huì)和服務(wù)器端交互,這就需要手機(jī)客戶端發(fā)送網(wǎng)絡(luò)請(qǐng)求,下面介紹四種常用網(wǎng)絡(luò)請(qǐng)求方式,我這邊是通過(guò)Android單元測(cè)試來(lái)完成這四種方法的,還不清楚Android的單元測(cè)試的同學(xué)們請(qǐng)看Android開(kāi)發(fā)技巧總結(jié)中的Android單元測(cè)試的步驟一文。

Java.NET包中的HttpURLConnection類

Get方式:

// Get方式請(qǐng)求 
public static void requestByGet() throws Exception { 
 String path = "http://m.fzitv.net/logins.jsp?id=helloworld&pwd=android"; 
 // 新建一個(gè)URL對(duì)象 
 URL url = new URL(path); 
 // 打開(kāi)一個(gè)HttpURLConnection連接 
 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
 // 設(shè)置連接超時(shí)時(shí)間 
 urlConn.setConnectTimeout(5 * 1000); 
 // 開(kāi)始連接 
 urlConn.connect(); 
 // 判斷請(qǐng)求是否成功 
 if (urlConn.getResponseCode() == HTTP_200) { 
  // 獲取返回的數(shù)據(jù) 
  byte[] data = readStream(urlConn.getInputStream()); 
  Log.i(TAG_GET, "Get方式請(qǐng)求成功,返回?cái)?shù)據(jù)如下:"); 
  Log.i(TAG_GET, new String(data, "UTF-8")); 
 } else { 
  Log.i(TAG_GET, "Get方式請(qǐng)求失敗"); 
 } 
 // 關(guān)閉連接 
 urlConn.disconnect(); 
} 

Post方式:

// Post方式請(qǐng)求 
public static void requestByPost() throws Throwable { 
 String path = "http://m.fzitv.net/logins.jsp"; 
 // 請(qǐng)求的參數(shù)轉(zhuǎn)換為byte數(shù)組 
 String params = "id=" + URLEncoder.encode("helloworld", "UTF-8") 
   + "&pwd=" + URLEncoder.encode("android", "UTF-8"); 
 byte[] postData = params.getBytes(); 
 // 新建一個(gè)URL對(duì)象 
 URL url = new URL(path); 
 // 打開(kāi)一個(gè)HttpURLConnection連接 
 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
 // 設(shè)置連接超時(shí)時(shí)間 
 urlConn.setConnectTimeout(5 * 1000); 
 // Post請(qǐng)求必須設(shè)置允許輸出 
 urlConn.setDoOutput(true); 
 // Post請(qǐng)求不能使用緩存 
 urlConn.setUseCaches(false); 
 // 設(shè)置為Post請(qǐng)求 
 urlConn.setRequestMethod("POST"); 
 urlConn.setInstanceFollowRedirects(true); 
 // 配置請(qǐng)求Content-Type 
 urlConn.setRequestProperty("Content-Type", 
   "application/x-www-form-urlencode"); 
 // 開(kāi)始連接 
 urlConn.connect(); 
 // 發(fā)送請(qǐng)求參數(shù) 
 DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream()); 
 dos.write(postData); 
 dos.flush(); 
 dos.close(); 
 // 判斷請(qǐng)求是否成功 
 if (urlConn.getResponseCode() == HTTP_200) { 
  // 獲取返回的數(shù)據(jù) 
  byte[] data = readStream(urlConn.getInputStream()); 
  Log.i(TAG_POST, "Post請(qǐng)求方式成功,返回?cái)?shù)據(jù)如下:"); 
  Log.i(TAG_POST, new String(data, "UTF-8")); 
 } else { 
  Log.i(TAG_POST, "Post方式請(qǐng)求失敗"); 
 } 
} 
 

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

Get方式:

// HttpGet方式請(qǐng)求 
public static void requestByHttpGet() throws Exception { 
 String path = "http://m.fzitv.net/logins.jsp?id=helloworld&pwd=android"; 
 // 新建HttpGet對(duì)象 
 HttpGet httpGet = new HttpGet(path); 
 // 獲取HttpClient對(duì)象 
 HttpClient httpClient = new DefaultHttpClient(); 
 // 獲取HttpResponse實(shí)例 
 HttpResponse httpResp = httpClient.execute(httpGet); 
 // 判斷是夠請(qǐng)求成功 
 if (httpResp.getStatusLine().getStatusCode() == HTTP_200) { 
  // 獲取返回的數(shù)據(jù) 
  String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); 
  Log.i(TAG_HTTPGET, "HttpGet方式請(qǐng)求成功,返回?cái)?shù)據(jù)如下:"); 
  Log.i(TAG_HTTPGET, result); 
 } else { 
  Log.i(TAG_HTTPGET, "HttpGet方式請(qǐng)求失敗"); 
 } 
} 

Post方式:

// HttpPost方式請(qǐng)求 
public static void requestByHttpPost() throws Exception { 
 String path = "http://m.fzitv.net/"; 
 // 新建HttpPost對(duì)象 
 HttpPost httpPost = new HttpPost(path); 
 // Post參數(shù) 
 List<NameValuePair> params = new ArrayList<NameValuePair>(); 
 params.add(new BasicNameValuePair("id", "helloworld")); 
 params.add(new BasicNameValuePair("pwd", "android")); 
 // 設(shè)置字符集 
 HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); 
 // 設(shè)置參數(shù)實(shí)體 
 httpPost.setEntity(entity); 
 // 獲取HttpClient對(duì)象 
 HttpClient httpClient = new DefaultHttpClient(); 
 // 獲取HttpResponse實(shí)例 
 HttpResponse httpResp = httpClient.execute(httpPost); 
 // 判斷是夠請(qǐng)求成功 
 if (httpResp.getStatusLine().getStatusCode() == HTTP_200) { 
  // 獲取返回的數(shù)據(jù) 
  String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); 
  Log.i(TAG_HTTPGET, "HttpPost方式請(qǐng)求成功,返回?cái)?shù)據(jù)如下:"); 
  Log.i(TAG_HTTPGET, result); 
 } else { 
  Log.i(TAG_HTTPGET, "HttpPost方式請(qǐng)求失敗"); 
 } 
} 

以上是一些部分代碼,測(cè)試的時(shí)候在測(cè)試類中運(yùn)行對(duì)應(yīng)的測(cè)試方法即可。完整代碼點(diǎn)這里下載:源碼下載

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

凉山| 岑溪市| 章丘市| 正镶白旗| 杭锦旗| 阳新县| 夏邑县| 修文县| 隆尧县| 牡丹江市| 任丘市| 石家庄市| 高雄县| 尼勒克县| 漳平市| 舞阳县| 当阳市| 兴山县| 太湖县| 浦江县| 东台市| 阿城市| 吉木萨尔县| 深水埗区| 阿图什市| 思南县| 昌宁县| 平顶山市| 应城市| 承德县| 庄浪县| 余干县| 深水埗区| 蒙阴县| 平遥县| 江永县| 彰武县| 贡觉县| 南宫市| 白银市| 布拖县|