Android中使用百度APIstore介面
百度APIStore對開發者而言是比較方便的工具,它提供了很多介面給我們。在這篇文章中將介紹APIstore的使用方法。
1.從百度APIdtore找到我們所需要的介面,以新聞為例。
2.使用HttpURLConnection方法訪問該介面,拿到資料用IO流讀取出來。需要注意的是:使用非同步任務類。
3.因為返回的資料型別是Json型別,所以我們應該使用Android解析Json型別的方法解析,具體步驟請參閱之前的部落格。
4.對解析出來的資料進行操作
程式碼部分:
在上面HttpUtil是單獨操作網路的類,返回文字內容程式碼如下:import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.SimpleAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import com.jokerloveallen.ch08.util.FileUtil; import com.jokerloveallen.ch08.util.HttpUtil; import com.jokerloveallen.ch08.util.UrlUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class HttpNewsActivity extends AppCompatActivity { private Spinner spinner; private TextView tv; private SimpleAdapter sa; private List<Map<String,String>> channelList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http_news); spinner = (Spinner) findViewById(R.id.channel); tv = (TextView) findViewById(R.id.news); channelList = new ArrayList<>(); sa = new SimpleAdapter(this,channelList, android.R.layout.simple_list_item_1, new String[]{"name"},new int[]{android.R.id.text1}); spinner.setAdapter(sa); new getChannel().execute(); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { Map ma = channelList.get(i); String channelName = (String) ma.get("name"); String channeId = (String) ma.get(ma.get("name")); String[] flag =new String[]{channelName,channeId}; new getConnet().execute(flag); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } //從網路返回內容 public class getConnet extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... strings) { String httpUrl = UrlUtil.NewsUrl; String httpArg = "channelId="+strings[1]+"&" +"channelName="+strings[0]+"&" +"title=&" +"page=1&" +"needContent=1&" +"needHtml=0"; httpUrl = httpUrl + "?" + httpArg; return HttpUtil.HttpGet(httpUrl); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s.equals("")){ tv.setText("沒有資料"); } else { tv.setText(s); } } } //從網路返回頻道 public class getChannel extends AsyncTask<Void,Void,String>{ @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s.equals("")){ Toast.makeText(getBaseContext(),"沒有資料",Toast.LENGTH_SHORT).show(); return; } try { JSONObject obj = new JSONObject(s); JSONObject obj1 = obj.getJSONObject("showapi_res_body"); JSONArray ja = obj1.getJSONArray("channelList"); for (int i =0;i<ja.length();i++){ JSONObject channelobj = (JSONObject) ja.get(i); String id = channelobj.getString("channelId"); String name = channelobj.getString("name"); Map map = new HashMap(); map.put("name",name); map.put(name,id); channelList.add(map); sa.notifyDataSetChanged(); } } catch (JSONException e) { e.printStackTrace(); } } @Override protected String doInBackground(Void... voids) { return HttpUtil.HttpGet(UrlUtil.ChannelUrl); } } }
需要注意的是,部分網路無法訪問到百度APIStore,所以本程式只是在網路可以訪問到APIstore時,才能使用。本程式沒有將最終的資料進行排版,只是獲取到最新的新聞。import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtil { public static String HttpGet(String...strings){ BufferedReader reader = null; StringBuilder sbf = new StringBuilder(); HttpURLConnection connection = null; try { URL url = new URL(strings[0]); connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); // 填入apikey到HTTP header connection.setRequestProperty("apikey", "5b46143955a4b1ff1b470a94315625cd"); connection.connect(); if(connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead1 = null; while ((strRead1 = reader.readLine()) != null) { sbf.append(strRead1); sbf.append("\r\n"); } } } catch (Exception e) { e.printStackTrace(); } finally { if (reader !=null ){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection !=null){ connection.disconnect(); } } return sbf.toString(); } }
相關推薦
Android中使用百度APIstore介面
百度APIStore對開發者而言是比較方便的工具,它提供了很多介面給我們。在這篇文章中將介紹APIstore的使用方法。 1.從百度APIdtore找到我們所需要的介面,以新聞為例。 2.使用HttpURLConnection方法訪問該介面,拿到資料用IO流讀取出來。需要注
android中百度定位、城市選擇列表,右側字母展示
好久好久沒光顧過自己空空的部落格了,做專案的時候都是逛著別人的部落格急著把功能實現,近來閒下來了總結總結。 這個城市選擇功能也是當時做專案急著實現從哪找來的框架不記得了,然後改改用到專案中來的。 非常感謝提供最初原始碼的博主,主要的區別是添加了搜尋功能、定位功能,把以前的操
android中百度地圖撒點標識後臺附近的人座標點及使用者頭像
主要的用途是讓當前使用者知道自己還有哪些使用者在使用此款app,為實現後續的約跑,聊天等功能做鋪墊。 要知道附近其他使用者必須先知道自己的定位,那就要用到之前文章中提到的定位功能,這裡不再講解。 呼叫後臺介面上傳當前使用者的x、y座標點。獲取後臺傳回的使用者的物件list。
Android 百度地圖開發(一)如何呼叫百度地圖介面和在專案中顯示百度地圖以及實現定位
二、下載百度地圖API庫 然後新增到專案中即可。 三、在專案清單AndroidMainifest.xml配置百度地圖API key和新增相關許可權 四、在專案呼叫百度地圖專案功能,這篇文章就首先講講顯示地圖和定位的功能 首先
關於android開發中百度地圖地址轉換座標資訊的教程
</pre>個人原創 轉載宣告即可!!<pre name="code" class="java">GeoCodeOption geoCodeOption = new GeoCodeOption (); GeoCoder gc = GeoCod
ASP.NET MVC5 中百度ueditor富文本編輯器的使用
編譯時間 文本編輯器 ida height git aid asp ueditor jquery 隨著網站信息發布內容越來越多,越來越重視美觀,富文本編輯就是不可缺少的了,眾多編輯器比較後我選了百度的ueditor富文本編輯器。 百度ueditor富文本編輯器分為兩種一種
Android LBS 百度地圖
代碼 百度地圖 百度定位 第一行代碼 andro 申請 地圖 .html 實驗 研究了幾個小時,左沖右突,實驗沒成功。http://www.cnblogs.com/hbuwyg/p/7062285.html 計劃按照郭老師的《第一行代碼》教程再試一次。 前面的測試雖然沒
Android studio 百度地圖開發(2)地圖定位
gcj02 settings tick all adding ext tope wid erro Android studio 百度地圖開發(2)地圖定位 email:[email protected]/* */ 開發環境:win7 64位
Android開放百度地圖集成
包名 ati dsd cdata 目前 cti key) 清單 result 1、創建應用 獲取AK (我理解為Application key) 通過百度賬號登錄百度地圖開放平臺,進入API控制臺 http://lbsyun.baidu.com/apiconsole/
android的百度地圖開發(一)
one rst dir 還要 遠程服務 存儲 返回 src copy 1,註冊百度開發者賬號 2,申請key ,註意開發版SH和發布版的SH 獲取開發版SHA1: 輸入命令:keytool -list -v -keystore debug.keystore,回車輸入密鑰
android的百度地圖開發(二) 定位
頻率 update 殺死 一次 ddr animate 語義 pri des 參考:http://blog.csdn.net/mr_wzc/article/details/51590485 第一步,初始化LocationClient類 //獲取地圖控件引用
Android使用百度地圖出現閃退及定位時顯示藍屏問題
ima pos mod cal targe req except bubuko and 目錄 1.Android使用百度地圖出現閃退 2.Android使用百度地圖定位出現藍屏問題 1.Android使用百度地圖出現閃退 一般情況下出現閃退
解決https網站商品中百度分享無法使用的問題
bds 圖片 無法使用 them href HA oba 修改 height 百度分享不支持HTTPS這件事由來已久,但近期ecshop各最新模板咨詢的比較多,我們就總結了解決方案。 第一步:下載百度分享必備文件 點此下載static.zip,將解壓(解壓密碼:theme
Android studio百度地圖demo出現230錯誤,key校驗失敗
但是 studio 不能 提示 baidumap tps man ole 文件 轉自daoxiaomianzi原文 Android studio 百度地圖demo出現230錯誤,key校驗失敗 使用AndroidStudio導入Baidu地圖的as版的demo,引入後,發
Android 整合百度地圖實現裝置定位
Android 整合百度地圖實現裝置定位步驟1:申請android 端SDK :http://lbsyun.baidu.com/步驟2:下載基礎版SDK步驟3:下載示例程式步驟4:開始整合:ak加入libs加入SDKInitializer.setCoordType(CoordType.BD09LL);圖示類
爬取百度翻譯介面
https://fanyi.baidu.com/v2transapi 會報錯 2 嘗試切換到移動端看看結果 https://fanyi.baidu.com/basetrans 這才是正確的介面 3 程式碼展示
呼叫百度翻譯介面
表單提交頁面ajax提交 百度翻譯 </textarea> 控制器頁面 <?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use Ap
js呼叫百度地圖介面繪製任意多邊形並獲取每個點的經緯度等
來自:https://blog.csdn.net/u013239236/article/details/52213977 侵刪 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type"
Android整合百度地圖-----顯示地圖、定位、長按顯示地址資訊
一、基礎部分 轉載請註明出處:https://blog.csdn.net/Hunter2916/article/details/82867205 1、下載百度地圖的SDK 下載地址:http://lbsyun.baidu.com/index.php?title=android-locsd