?
?
?
?
?
?
?
?
--------- 部分實(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
- ? public ?String?doGet()??
- ????{??
- ????????String?uriAPI?=? "http://XXXXX?str=I+am+get+String" ;??
- ????????String?result=? "" ;??
- //??????HttpGet?httpRequst?=?new?HttpGet(URI?uri); ??
- //??????HttpGet?httpRequst?=?new?HttpGet(String?uri); ??
- //??????創(chuàng)建HttpGet或HttpPost對象,將要請求的URL通過構(gòu)造方法傳入HttpGet或HttpPost對象。 ??
- ????????HttpGet?httpRequst?=? new ?HttpGet(uriAPI);??
- ??
- //??????new?DefaultHttpClient().execute(HttpUriRequst?requst); ??
- ???????? try ?{??
- ??? //使用DefaultHttpClient類的execute方法發(fā)送HTTP?GET請求,并返回HttpResponse對象。 ??
- ????????????HttpResponse?httpResponse?=? new ?DefaultHttpClient().execute(httpRequst); //其中HttpGet是HttpUriRequst的子類 ??
- ???????????? if (httpResponse.getStatusLine().getStatusCode()?==? 200 )??
- ????????????{??
- ????????????????HttpEntity?httpEntity?=?httpResponse.getEntity();??
- ????????????????result?=?EntityUtils.toString(httpEntity); //取出應(yīng)答字符串 ??
- ???????????? //?一般來說都要?jiǎng)h除多余的字符? ??
- ????????????????result.replaceAll( "\r" ,? "" ); //去掉返回結(jié)果中的"\r"字符,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格?? ??
- ????????????}??
- ??????????????????? else ???
- ????????????????????????httpRequst.abort();??
- ???????????}? catch ?(ClientProtocolException?e)?{??
- ???????????? //?TODO?Auto-generated?catch?block ??
- ????????????e.printStackTrace();??
- ????????????result?=?e.getMessage().toString();??
- ????????}? catch ?(IOException?e)?{??
- ???????????? //?TODO?Auto-generated?catch?block ??
- ????????????e.printStackTrace();??
- ????????????result?=?e.getMessage().toString();??
- ????????}??
- ???????? return ?result;??
- ????}??
?
????????????? ? HttpPost
????????????? 如果使用 HttpPost 方法提交 HTTP?POST 請求,則需要使用 HttpPost 類的 setEntity 方法設(shè)置請求參數(shù)。參數(shù)則必須用NameValuePair[]數(shù)組存儲(chǔ)。
- public ?String?doPost()??
- ????{??
- ????????String?uriAPI?=? "http://XXXXXX" ;//Post方式?jīng)]有參數(shù)在這里??
- ????????String?result?=? "" ;??
- ????????HttpPost?httpRequst?=? new ?HttpPost(uriAPI); //創(chuàng)建HttpPost對象 ??
- ??????????
- ????????List?<NameValuePair>?params?=? new ?ArrayList<NameValuePair>();??
- ????????params.add( new ?BasicNameValuePair( "str" ,? "I?am?Post?String" ));??
- ??????????
- ???????? try ?{??
- ????????????httpRequst.setEntity( new ?UrlEncodedFormEntity(params,HTTP.UTF_8));??
- ????????????HttpResponse?httpResponse?=? new ?DefaultHttpClient().execute(httpRequst);??
- ???????????? if (httpResponse.getStatusLine().getStatusCode()?==? 200 )??
- ????????????{??
- ????????????????HttpEntity?httpEntity?=?httpResponse.getEntity();??
- ????????????????result?=?EntityUtils.toString(httpEntity); //取出應(yīng)答字符串 ??
- ????????????}??
- ????????}? catch ?(UnsupportedEncodingException?e)?{??
- ???????????? //?TODO?Auto-generated?catch?block ??
- ????????????e.printStackTrace();??
- ????????????result?=?e.getMessage().toString();??
- ????????}??
- ???????? catch ?(ClientProtocolException?e)?{??
- ???????????? //?TODO?Auto-generated?catch?block ??
- ????????????e.printStackTrace();??
- ????????????result?=?e.getMessage().toString();??
- ????????}??
- ???????? catch ?(IOException?e)?{??
- ???????????? //?TODO?Auto-generated?catch?block ??
- ????????????e.printStackTrace();??
- ????????????result?=?e.getMessage().toString();??
- ????????}??
- ???????? return ?result;??
- ????}??
?
????????? 以發(fā)送連接請求時(shí),需要設(shè)置鏈接超時(shí)和請求超時(shí)等參數(shù),否則會(huì)長期停止或者崩潰。
- HttpParams?httpParameters?=? new ?BasicHttpParams();??
- HttpConnectionParams.setConnectionTimeout(httpParameters,? 10 * 1000 ); //設(shè)置請求超時(shí)10秒 ??
- HttpConnectionParams.setSoTimeout(httpParameters,? 10 * 1000 );? //設(shè)置等待數(shù)據(jù)超時(shí)10秒 ??
- HttpConnectionParams.setSocketBufferSize(params,? 8192 );??
- HttpClient?httpclient?=? new ?DefaultHttpClient(httpParameters);? //此時(shí)構(gòu)造DefaultHttpClient時(shí)將參數(shù)傳入 ??
- ??
- ??
- ??
- 由于是聯(lián)網(wǎng),在AndroidManifest.xml中添加網(wǎng)絡(luò)連接的權(quán)限??
-
<uses-permission?android:name=
"android.permission.INTERNET"
/>?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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