亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

HTTPClient模塊的HttpGet和HttpPost

系統(tǒng) 1896 0

?

?

?

?

?

?

?

?

--------- 部分實(shí)例

?

try {
??? ??? ??? ??? ??? HttpResponse response = client.execute(myget);
??? ??? ??? ??? ??? BufferedReader reader = new BufferedReader(
??? ??? ??? ??? ??? ??? ??? new InputStreamReader(response.getEntity()
??? ??? ??? ??? ??? ??? ??? ??? ??? .getContent()));
??? ??? ??? ??? ?? ? for (String s = reader.readLine(); s != null; s = reader
??? ??? ??? ??? ??? ??? ??? .readLine()) {
??? ??? ??? ??? ??? ?? ? builder.append(s);
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? JSONObject jsonObject = new JSONObject(builder.toString());
??? ??? ??? ??? ??? error = jsonObject.getInt("errno");
??? ??? ??? ??? ??? String errormsg = jsonObject.getString("errmsg");
??? ??? ??? ??? ??? Log.v("wgp",myget.getURI().toString());
??? ??? ??? ??? ??? Log.v("wgp", "error=" + error);
??? ??? ??? ??? ??? Log.v("wgp", "errormsg=" + errormsg);
??? ??? ??? ??? ??? if (error == 0) {
??? ??? ??? ??? ??? ??? array = jsonObject.getJSONArray("data");
??? ??? ??? ??? ??? ??? mFavoriteList = new ArrayList<FavoriteItem>();
??? ??? ??? ??? ??? ??? FavoriteItem item = null;
??? ??? ??? ??? ??? ??? String srcType;
??? ??? ??? ??? ??? ??? for (int i = 0; i < array.length(); i++) {
??? ??? ??? ??? ??? ??? ??? item = new FavoriteItem();
??? ??? ??? ??? ??? ??? ??? item.setId(array.getJSONObject(i).getInt("id"));
??? ??? ??? ??? ??? ??? ??? item.setUserId(array.getJSONObject(i).getInt("userid"));
??? ??? ??? ??? ??? ??? ??? item.setUsername(array.getJSONObject(i).getString("username"));
??? ??? ??? ??? ??? ??? ??? item.setType(array.getJSONObject(i).getInt("type"));
??? ??? ??? ??? ??? ??? ??? item.setCid(array.getJSONObject(i).getInt("cid"));
??? ??? ??? ??? ??? ??? ??? item.setTitle(array.getJSONObject(i).getString("title"));
??? ??? ??? ??? ??? ??? ??? item.setUrl(array.getJSONObject(i).getString("url"));
??? ??? ??? ??? ??? ??? ??? item.setTag(array.getJSONObject(i).getString("tag"));
??? ??? ??? ??? ??? ??? ??? item.setSrc(array.getJSONObject(i).getString("src"));
??? ??? ??? ??? ??? ??? ??? item.setCreateDate(array.getJSONObject(i).getString("created"));
??? ??? ??? ??? ??? ??? ??? item.setModifiedDate(array.getJSONObject(i).getString("modified"));
??? ??? ??? ??? ??? ??? ??? srcType = array.getJSONObject(i).getString("src_type");
??? ??? ??? ??? ??? ??? ??? if(srcType != null && srcType.equals("cps")){
??? ??? ??? ??? ??? ??? ??? ??? item.setSrc_type("industry");
??? ??? ??? ??? ??? ??? ??? }else{
??? ??? ??? ??? ??? ??? ??? ??? item.setSrc_type(srcType);
??? ??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? ??? mFavoriteList.add(item);
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? ??? if (mFavoriteList.size() == 0) {
??? ??? ??? ??? ??? ??? ??? handler.sendEmptyMessage(-1);
??? ??? ??? ??? ??? ??? } else {
??? ??? ??? ??? ??? ??? ??? handler.sendEmptyMessage(1);//更新主UI
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? }
??? ??? ??? ??? } catch (Exception e) {
??? ??? ??? ??? ??? e.printStackTrace();
??? ??? ??? ??? ??? handler.sendEmptyMessage(-1);
??? ??? ??? ??? } finally {
??? ??? ??? ??? }

?

?

?

?

=============

?

?

?

?

?

Android?SDK集成了Apache?HttpClient模塊。要注意的是,這里的Apache?HttpClient模塊是 HttpClient?4.0(org.apache.http.*),而不是常見的 Jakarta?Commons?HttpClient?3.x(org.apache.commons.httpclient.*)。

?????????? HttpClient常用HttpGet和HttpPost這兩個(gè)類,分別對應(yīng)Get方式和Post方式。

?

?

?????????? 無論是使用 HttpGet ,還是使用 HttpPost ,都必須通過如下 3 步來訪問 HTTP 資源。

?

?????????? 1. 創(chuàng)建 HttpGet HttpPost 對象,將要請求的 URL 通過構(gòu)造方法傳入 HttpGet HttpPost 對象。

???????? ? 2. 使用 DefaultHttpClient 類的 execute 方法發(fā)送 HTTP?GET HTTP?POST 請求,并返回 HttpResponse 對象。

?????????? 3. 通過 HttpResponse 接口的 getEntity 方法返回響應(yīng)信息,并進(jìn)行相應(yīng)的處理。

?????????? 如果使用 HttpPost 方法提交 HTTP?POST 請求,則需要使用 HttpPost 類的 setEntity 方法設(shè)置請求參數(shù)。參數(shù)則必須用NameValuePair[]數(shù)組存儲(chǔ)。

??????????

???????? ?? HttpGet

  1. ? public ?String?doGet()??
  2. ????{??
  3. ????????String?uriAPI?=? "http://XXXXX?str=I+am+get+String" ;??
  4. ????????String?result=? "" ;??
  5. //??????HttpGet?httpRequst?=?new?HttpGet(URI?uri); ??
  6. //??????HttpGet?httpRequst?=?new?HttpGet(String?uri); ??
  7. //??????創(chuàng)建HttpGet或HttpPost對象,將要請求的URL通過構(gòu)造方法傳入HttpGet或HttpPost對象。 ??
  8. ????????HttpGet?httpRequst?=? new ?HttpGet(uriAPI);??
  9. ??
  10. //??????new?DefaultHttpClient().execute(HttpUriRequst?requst); ??
  11. ???????? try ?{??
  12. ??? //使用DefaultHttpClient類的execute方法發(fā)送HTTP?GET請求,并返回HttpResponse對象。 ??
  13. ????????????HttpResponse?httpResponse?=? new ?DefaultHttpClient().execute(httpRequst); //其中HttpGet是HttpUriRequst的子類 ??
  14. ???????????? if (httpResponse.getStatusLine().getStatusCode()?==? 200 )??
  15. ????????????{??
  16. ????????????????HttpEntity?httpEntity?=?httpResponse.getEntity();??
  17. ????????????????result?=?EntityUtils.toString(httpEntity); //取出應(yīng)答字符串 ??
  18. ???????????? //?一般來說都要?jiǎng)h除多余的字符? ??
  19. ????????????????result.replaceAll( "\r" ,? "" ); //去掉返回結(jié)果中的"\r"字符,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格?? ??
  20. ????????????}??
  21. ??????????????????? else ???
  22. ????????????????????????httpRequst.abort();??
  23. ???????????}? catch ?(ClientProtocolException?e)?{??
  24. ???????????? //?TODO?Auto-generated?catch?block ??
  25. ????????????e.printStackTrace();??
  26. ????????????result?=?e.getMessage().toString();??
  27. ????????}? catch ?(IOException?e)?{??
  28. ???????????? //?TODO?Auto-generated?catch?block ??
  29. ????????????e.printStackTrace();??
  30. ????????????result?=?e.getMessage().toString();??
  31. ????????}??
  32. ???????? return ?result;??
  33. ????}??

?

????????????? ? HttpPost

????????????? 如果使用 HttpPost 方法提交 HTTP?POST 請求,則需要使用 HttpPost 類的 setEntity 方法設(shè)置請求參數(shù)。參數(shù)則必須用NameValuePair[]數(shù)組存儲(chǔ)。

  1. public ?String?doPost()??
  2. ????{??
  3. ????????String?uriAPI?=? "http://XXXXXX" ;//Post方式?jīng)]有參數(shù)在這里??
  4. ????????String?result?=? "" ;??
  5. ????????HttpPost?httpRequst?=? new ?HttpPost(uriAPI); //創(chuàng)建HttpPost對象 ??
  6. ??????????
  7. ????????List?<NameValuePair>?params?=? new ?ArrayList<NameValuePair>();??
  8. ????????params.add( new ?BasicNameValuePair( "str" ,? "I?am?Post?String" ));??
  9. ??????????
  10. ???????? try ?{??
  11. ????????????httpRequst.setEntity( new ?UrlEncodedFormEntity(params,HTTP.UTF_8));??
  12. ????????????HttpResponse?httpResponse?=? new ?DefaultHttpClient().execute(httpRequst);??
  13. ???????????? if (httpResponse.getStatusLine().getStatusCode()?==? 200 )??
  14. ????????????{??
  15. ????????????????HttpEntity?httpEntity?=?httpResponse.getEntity();??
  16. ????????????????result?=?EntityUtils.toString(httpEntity); //取出應(yīng)答字符串 ??
  17. ????????????}??
  18. ????????}? catch ?(UnsupportedEncodingException?e)?{??
  19. ???????????? //?TODO?Auto-generated?catch?block ??
  20. ????????????e.printStackTrace();??
  21. ????????????result?=?e.getMessage().toString();??
  22. ????????}??
  23. ???????? catch ?(ClientProtocolException?e)?{??
  24. ???????????? //?TODO?Auto-generated?catch?block ??
  25. ????????????e.printStackTrace();??
  26. ????????????result?=?e.getMessage().toString();??
  27. ????????}??
  28. ???????? catch ?(IOException?e)?{??
  29. ???????????? //?TODO?Auto-generated?catch?block ??
  30. ????????????e.printStackTrace();??
  31. ????????????result?=?e.getMessage().toString();??
  32. ????????}??
  33. ???????? return ?result;??
  34. ????}??

?

????????? 以發(fā)送連接請求時(shí),需要設(shè)置鏈接超時(shí)和請求超時(shí)等參數(shù),否則會(huì)長期停止或者崩潰。

  1. HttpParams?httpParameters?=? new ?BasicHttpParams();??
  2. HttpConnectionParams.setConnectionTimeout(httpParameters,? 10 * 1000 ); //設(shè)置請求超時(shí)10秒 ??
  3. HttpConnectionParams.setSoTimeout(httpParameters,? 10 * 1000 );? //設(shè)置等待數(shù)據(jù)超時(shí)10秒 ??
  4. HttpConnectionParams.setSocketBufferSize(params,? 8192 );??
  5. HttpClient?httpclient?=? new ?DefaultHttpClient(httpParameters);? //此時(shí)構(gòu)造DefaultHttpClient時(shí)將參數(shù)傳入 ??
  6. ??
  7. ??
  8. ??
  9. 由于是聯(lián)網(wǎng),在AndroidManifest.xml中添加網(wǎng)絡(luò)連接的權(quán)限??
  10. <uses-permission?android:name= "android.permission.INTERNET" />?

HTTPClient模塊的HttpGet和HttpPost


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人啪精品视频免费网 | 亚洲狠狠色丁香婷婷综合 | 大毛片a大毛片 | 狠狠激情| 日韩人成免费网站大片 | 亚洲综合日韩在线亚洲欧美专区 | 目韩一区二区三区系列片丶 | 久久99精品久久久久久牛牛影视 | 天天射狠狠干 | 欧美一级毛片免费高清的 | 久久国产精品只做精品 | 国产欧美一区视频在线观看 | 欧美成人天天综合天天在线 | 国产日韩久久久久69影院 | 欧美激情综合亚洲一二区 | 国产成人a v在线影院 | 成人免费观看网站 | 久久99精品久久久久久黑人 | 亚洲最大免费视频网 | 国内精品久久久久久不卡影院 | 国产亚洲综合一区二区在线 | 亚洲在线视频免费观看 | 性xxxxxx| 福利视频一区二区 | 九九热最新网址 | 亚洲一区二区三区免费观看 | 色综合视频在线 | 欧美精品色视频 | 国产成人精品999在线 | 激情综合网五月 | 欧美成人性生活视频 | 99久久精品6在线播放 | 成人午夜视频免费看欧美 | 天天操夜夜夜 | 中日韩欧美一级毛片 | 日本免费不卡 | 香蕉免费一区二区三区在线观看 | 看黄a大片 免费 | 成人午夜网站 | 国产精品伦理久久久久久 | 国产成人精品自拍 |