Home > front end >  Pass YouTube API JSONObject Result to Method
Pass YouTube API JSONObject Result to Method

Time:10-21

I have been using GitHub PierfrancescoSoffritti android-youtube-player. The main code uses a videoID to play. However, I would like to use it for a YouTube Livestream, and videoId can change if the stream is ever recreated.

String videoId = "(ID of YouTube Video)";
                youTubePlayer.loadVideo(videoId, 0);

So I am trying to use GitHub Volley to make the http request to the google YouTube API to get the latest videoID of the livestream.

String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=(MyChannelID)&eventType=live&type=video&key=(MyKey)";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        ytapi.setText("Response: "   response.toString());
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO: Handle error
                    }
                });

This returns something along the lines of:

"items": [
    {
      "kind": "youtube#searchResult",
      "etag": "RkUpUQpSKztQlA",
      "id": {
        "kind": "youtube#video",
        "videoId": "GLdex45V_RQ"

Currently, I am just displaying this in a TextView

final TextView ytapi = view.findViewById(R.id.textYouTubeAPI);

So my question is how can I pass the "videoId": (shown as "GLdex45V_RQ" in the returned JSONObject example above) to the loadVideo() method of the youtubeplayer. I assume I need to parse the JSONObject for the videoId result and pass it, but this is where I am stuck.

String videoId = "(videoid from JSONObject)";
                youTubePlayer.loadVideo(videoId, 0);

I have tried something along the lines of

 String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=(MyChannelID)&eventType=live&type=video&key=(MyKey)";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("items");
                            for (int i = 0; i < jsonArray.length(); i  ) {
                                JSONObject id = jsonArray.getJSONObject(i);
                                String VideoID = id.getString("videoId");
                                ytapi.setText(VideoID);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener()
        { 
mQueue.add(request);

But get null object reference on com.android.volley.Request

What finally worked to return a youtube livestream videoid using Volley, Google YouTube API, and JSON Response.

try {
                            JSONArray jsonArray = response.getJSONArray("items");

                            for (int i = 0; i < jsonArray.length(); i  ) {
                                JSONObject jsonObject = jsonArray.optJSONObject(i);
                                JSONObject jsonArray1 = jsonObject.getJSONObject("id");
                                String videoid = jsonArray1.optString("videoId");
                                ytapi.setText(videoid);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

CodePudding user response:

My solution was missing the array.

try {
                            JSONArray jsonArray = response.getJSONArray("items");

                            for (int i = 0; i < jsonArray.length(); i  ) {
                                JSONObject jsonObject = jsonArray.optJSONObject(i);
                                JSONObject jsonArray1 = jsonObject.getJSONObject("id");
                                String videoid = jsonArray1.optString("videoId");
                                ytapi.setText(videoid);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
  • Related