Ricochet
Ricochet

Reputation: 75

Value [YoutubePlaylistID] at id of type java.lang.String cannot be converted to JSONObject

I am trying to create an application that will use the YoutTube Api to get all the videoIDs from a YouTube playlist. Then, the application will, hopefully, display all the videoIDs in a listView.

I am using Volley to handle all the HTTPs requests, I've added all the Google Api dependencies to my project, I've registered my app on the Google Developer Console and I've asked for Internet Permission in my Android Manifest file.

However, I run into this error ("PLTpRNmzKHx18bIns2RMyw9czfPIn8SNpf" is the ID of my playlist):

Run Error

W/System.err: org.json.JSONException: Value PLTpRNmzKHx18bIns2RMyw9czfPIn8SNpf at id of type java.lang.String cannot be converted to JSONObject
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:101)
        at org.json.JSONObject.getJSONObject(JSONObject.java:616)
        at com.utilities.dog.MainActivity$1.onResponse(MainActivity.java:102)
        at com.utilities.dog.MainActivity$1.onResponse(MainActivity.java:93)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6810)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

I've seen people online getting the same problem. Apparently, defining a String variable before calling ".getJSONObject" can solve the problem. However, it did not worked for me so I reversed my code back to what it was.

Please take a look at my code :

MainActivity.java

ublic class MainActivity extends AppCompatActivity {

    [...]

    // Playlist URL I want to get data from.
    String url = "https://www.googleapis.com/youtube/v3/playlists?part=contentDetails%2C%20snippet%2C%20id&id=PLTpRNmzKHx18bIns2RMyw9czfPIn8SNpf&maxResults=25&key=[My API Key is here]";    

    private void getVideoIDs()
    {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try{
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("items");

                    for (int i = 0; i<jsonArray.length(); i++){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        JSONObject jsonVideoId =  jsonObject1.getJSONObject("id");
                        String video_id = jsonVideoId.getString("videoID");
                        VideoDetails vd = new VideoDetails();
                        vd.setTitle(video_id);
                        videoDetailsArrayList.add(vd);
                        Toast.makeText(getApplicationContext(), video_id, Toast.LENGTH_SHORT).show();
                    }
                    listView.setAdapter(myCustomAdapter);
                    myCustomAdapter.notifyDataSetChanged();
                }catch(JSONException e){
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

        requestQueue.add(stringRequest);
    }
}

VideoDetails.java is a public class with getter and setter method for two string variables (title, videoId)

I am also wondering if I do need to call both 'ContentDetails' and 'Snippet' in order to get VideoIDs from a playlist or if I can just call one of the two.

Hope you can help me!

Upvotes: 0

Views: 72

Answers (1)

stvar
stvar

Reputation: 6965

When needed to get all video IDs of a given playlist (in your case PLTpRNmzKHx18bIns2RMyw9czfPIn8SNpf), you should query the PlaylistItems.list endpoint instead of Playlists.list endpoint.

This amounts to replacing playlists with playlistItems and id with playlistId in the invoking URL:

https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails%2C%20snippet%2C%20id&playlistId=PLTpRNmzKHx18bIns2RMyw9czfPIn8SNpf&maxResults=25&key=[My API Key is here]

If you only need video IDs, then suffices to have the part parameter be part=ContentDetails. Those video IDs are to be found at items[].contentDetails.videoId.

Upvotes: 1

Related Questions