欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

HTTPClient模塊的HttpGet和HttpPost

系統 1963 0

?

?

?

?

?

?

?

?

--------- 部分實例

?

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這兩個類,分別對應Get方式和Post方式。

?

?

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

?

?????????? 1. 創建 HttpGet HttpPost 對象,將要請求的 URL 通過構造方法傳入 HttpGet HttpPost 對象。

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

?????????? 3. 通過 HttpResponse 接口的 getEntity 方法返回響應信息,并進行相應的處理。

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

??????????

???????? ?? 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. //??????創建HttpGet或HttpPost對象,將要請求的URL通過構造方法傳入HttpGet或HttpPost對象。 ??
  8. ????????HttpGet?httpRequst?=? new ?HttpGet(uriAPI);??
  9. ??
  10. //??????new?DefaultHttpClient().execute(HttpUriRequst?requst); ??
  11. ???????? try ?{??
  12. ??? //使用DefaultHttpClient類的execute方法發送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); //取出應答字符串 ??
  18. ???????????? //?一般來說都要刪除多余的字符? ??
  19. ????????????????result.replaceAll( "\r" ,? "" ); //去掉返回結果中的"\r"字符,否則會在結果字符串后面顯示一個小方格?? ??
  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 方法設置請求參數。參數則必須用NameValuePair[]數組存儲。

  1. public ?String?doPost()??
  2. ????{??
  3. ????????String?uriAPI?=? "http://XXXXXX" ;//Post方式沒有參數在這里??
  4. ????????String?result?=? "" ;??
  5. ????????HttpPost?httpRequst?=? new ?HttpPost(uriAPI); //創建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); //取出應答字符串 ??
  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. ????}??

?

????????? 以發送連接請求時,需要設置鏈接超時和請求超時等參數,否則會長期停止或者崩潰。

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

HTTPClient模塊的HttpGet和HttpPost


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美无遮挡一区二区三区 | 国产亚洲精品久久久999无毒 | 日韩欧美精品在线 | 欧美精品在线观看视频 | 丝袜捆绑调教视频免费区 | 欧美大片在线免费观看 | 一级做a | 午夜视频网址 | 国产精品成人免费观看 | 黄色小视频在线观看 | 久久www免费人成精品 | 九九综合视频 | 久久综合一区二区三区 | 成人免费在线视频观看 | 欧美一做特黄毛片 | 91在线免费看 | 久综合网 | 久久久久亚洲 | 国产伦精品一区二区三区四区视频 | 精品国产影院 | 亚洲精品黄色 | 久久久综合 | 精品国产不卡一区二区三区 | 欧美在线一区二区三区 | www.久久久.com | 国产午夜免费一区二区三区 | 日韩欧美一区二区三区不卡在线 | 成人免费观看国产高清 | 国产a精品三级 | 国产福利在线播放 | 亚洲精品在线播放 | 日韩电影免费在线观看中文字幕 | 欧洲一级毛片 | 天天干天天操天天做 | 精品视频在线观看 | www.av在线免费观看 | 天天综合网在线观看视频 | 日本黄色大片免费 | 欧美日韩国产手机在线观看视频 | 在线观看免费国产视频 | 国产一级大片在线观看 |