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

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條評論
主站蜘蛛池模板: 九九精品国产99精品 | 国内精品福利在线视频 | 免费一极毛片 | 中文字幕一区二区精品区 | 高清成人综合 | 国产香蕉国产精品偷在线观看 | 国产精品国产精品国产专区不卡 | 国产美女一级高清免费观看 | 久热精品在线视频 | 九九干 | 成年人黄色在线观看 | 狠狠躁天天躁夜夜躁婷婷 | 中文字幕在线免费视频 | 俄罗斯色视频 | 99精品在线免费 | 久久亚洲精品视频 | 欧美亚洲国产日韩综合在线播放 | 国产精品一级片 | 香蕉视频在线观看视频 | 深夜男人网站 | 久久欧美精品欧美九久欧美 | 国产亚洲一区二区三区啪 | 4hu四虎| 精品久草 | 九九久久免费视频 | 欧美精品一区二区三区久久 | 久久影| 天天爽天天碰狠狠添 | 国产青草亚洲香蕉精品久久 | 99热这里只有精品国产动漫 | 99j久久精品久久久久久 | 四虎影视在线影院在线观看观看 | 久久这里只有精品免费视频 | www.青草| 久久精品国产无限资源 | 国产在线一区二区三区欧美 | 操操操人人| 久久亚洲精品人成综合网 | 日本欧洲亚洲一区在线观看 | 久久九九热| 四虎国产精品成人永久免费影视 |