Presentation is loading. Please wait.

Presentation is loading. Please wait.

透過YouTuBe API取得資料 建國科技大學 資管系 饒瑞佶 2018/1 V1.

Similar presentations


Presentation on theme: "透過YouTuBe API取得資料 建國科技大學 資管系 饒瑞佶 2018/1 V1."— Presentation transcript:

1 透過YouTuBe API取得資料 建國科技大學 資管系 饒瑞佶 2018/1 V1

2 改用Glide函式庫載入圖片 加入 compile 'com.github.bumptech.glide:glide:3.4.+'
修改CardPresenter Glide.with(mContext) .load(uri.toString()) .centerCrop() .error(mDefaultCardImage) .into(mCardView.getMainImageView());

3 取得相關影片API // 相關影片 private void callrelated(String videoid) { try { String strURI = " + videoid + "&type=video&key=xxxxxxxxxxxxxxxxx"; new DownloadTask().execute(strURI); // 下載相關影片資料 } catch (Exception ex) { }

4 result

5 JSON JavaScript Object Notation 一種用於資料傳輸與交換的輕量級語法 以文字為主

6 JSON sample Key-value pair var employee = { "fullname" : "Sam Kelly",
"telephones" : [ {"type" : "work" , "value" : " "}, {"type" : "home" , "value" : " "}, ], "addresses" : [ {"type" : "work" , "value" : "11 1st Ave"}, {"type" : "home" , "value" : "22 Main St"}, ] }

7 JSON中的幾個符號 :  用以分隔名稱與資料 { } 用來表示資料集(物件) [] 用來表示陣列 ,  用來分割每段「資料對」

8 JSON格式 物件 (object) 一個物件以「{」開始,並以「}」結束。 一個物件包含一系列非排序的名稱/值對
每個名稱/值對之間使用「,」分割 名稱/值(collection) 名稱和值之間使用「:」隔開 名稱為一字串 值為字串、數值、物件、布林值、陣列或是null {name:value} 筆資料使用「,」分隔 陣列(Array) 使用中括弧「[ ]」將資料收集 多筆資料使用「,」分隔 [collection, collection, collection]

9 JSON解析 Google提供org.json.JSONObject類別 JSONObject(要解析的原字串) get(name)
getJSONArray(name) 透過getJSONObject(index)取得JSONObject物件 getXXX(name), 如Int, Double, String…等 取得name對應的資料(依據資料格式)

10 DownloadTask private class DownloadTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { return downloadUrl(urls[0]); } protected void onPostExecute(String result) { try { HeaderItem cardPresenterHeader = new HeaderItem(2, "YouTuBe資料"); CardPresenter cardPresenter = new CardPresenter(); ArrayObjectAdapter cardRowAdapter = new ArrayObjectAdapter(cardPresenter);

11 JSONObject json = new JSONObject(result);
String data = json.getString("items"); JSONArray dataArray = new JSONArray(data); for (int i = 0; i < dataArray.length(); i++) { Movie movie = new Movie(); movie.setVideoID(dataArray.getJSONObject(i).getJSONObject("id").getString("videoId")); movie.setTitle(dataArray.getJSONObject(i).getJSONObject("snippet").getString("title")); movie.setStudio(dataArray.getJSONObject(i).getJSONObject("snippet").getString("channelTitle")); movie.setDescription(dataArray.getJSONObject(i).getJSONObject("snippet").getString("description")); if (!dataArray.getJSONObject(i).getJSONObject("snippet").isNull("thumbnails")) { movie.setCardImageUrl(dataArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("default").getString("url")); } else { movie.setCardImageUrl(""); } cardRowAdapter.add(movie); mRowsAdapter.add(new ListRow(cardPresenterHeader, cardRowAdapter)); // 設定整個Fragment得資料來源是mRowsAdapter setAdapter(mRowsAdapter); } catch (Exception e) { e.printStackTrace();

12 downloadUrl private String downloadUrl(String urlString) {
String strHTML = ""; try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); // milliseconds conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); InputStream stream = conn.getInputStream();

13 if (stream != null) { int leng = 0; byte[] Data = new byte[200]; byte[] totalData = new byte[0]; int totalLeg = 0; do { leng = stream.read(Data); if (leng > 0) { totalLeg += leng; byte[] temp = new byte[totalLeg]; System.arraycopy(totalData, 0, temp, 0, totalData.length); System.arraycopy(Data, 0, temp, totalData.length, leng); totalData = temp; } while (leng > 0); ; strHTML = new String(totalData, "UTF-8"); } catch (Exception e) { e.printStackTrace(); return strHTML;

14 修正原MainFragment

15 使用playlist取得影片 範例 playlistId=PLnCLSKesstE2ymsTaIlcTZK_0I8JfCKx7

16 result 格式與前面有點不同 主要是在videoid部分

17 取單單一影片的資訊

18 result


Download ppt "透過YouTuBe API取得資料 建國科技大學 資管系 饒瑞佶 2018/1 V1."

Similar presentations


Ads by Google