Example #1
0
    @Override
    protected void onPostExecute(String result) {

      if (isCancelled()) return;
      else {
        if (isNew) {
          youtubeChannelSearch.clear();
          searchAdapter.notifyDataSetChanged();
        }
        try {
          JSONObject res = new JSONObject(response);

          if (Var.isJsonString(res, "nextPageToken")) pageToken = res.getString("nextPageToken");

          if (Var.isJsonArray(res, "items")) {
            JSONArray items = res.getJSONArray("items");
            for (int i = 0; i < items.length(); i++) {
              JSONObject item = items.getJSONObject(i);

              YoutubeChannel channel = new YoutubeChannel();
              if (Var.isJsonObject(item, "id")) { // Channel Id
                JSONObject id = item.getJSONObject(("id"));
                if (Var.isJsonString(id, "channelId")) {
                  channel.setFeedId(id.getString("channelId"));
                }
              }

              if (Var.isJsonObject(item, "snippet")) { // Channel Name
                JSONObject snippet = item.getJSONObject("snippet");
                if (Var.isJsonString(snippet, "title")) {
                  channel.setName(snippet.getString("title"));
                }

                if (Var.isJsonObject(snippet, "thumbnails")) { // Channel Thumbnail
                  JSONObject thumbnails = snippet.getJSONObject("thumbnails");
                  if (Var.isJsonObject(thumbnails, "default")) {
                    JSONObject def = thumbnails.getJSONObject("default");
                    if (Var.isJsonString(def, "url")) {
                      channel.setThumbnail(def.getString("url"));
                    }
                  }
                }

                youtubeChannelSearch.add(channel);
              }
            }
            searchAdapter.notifyDataSetChanged();
            if (youtubeChannelSearch.size() >= maxResults)
              searchBusy = false; // Keep busy if nothing is returned
          }

        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    }