Reputation: 11
Error while running my project
E/ChannelActivity: New Video IdjxaQXUCdIt0
New Video IdSz6U9KWa8s8
W/System.err: org.json.JSONException: No value for videoId
at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
at android.com.demo.ChannelActivity$1.onResponse(ChannelActivity.java:73)
at android.com.demo.ChannelActivity$1.onResponse(ChannelActivity.java:56)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
Here is my JSONObject Code
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");
JSONObject jsonsnippet= jsonObject1.getJSONObject("snippet");
JSONObject jsonObjectdefault = jsonsnippet.getJSONObject("thumbnails").getJSONObject("medium");
String videoid=jsonVideoId.getString("videoId");
VideoDetails videoDetails=new VideoDetails();
videoDetails.setVideoid(videoid);
videoDetails.setTitle(jsonsnippet.getString("title"));
videoDetails.setDescribtion(jsonsnippet.getString("description"));
videoDetails.setUrl(jsonObjectdefault.getString("url"));
videoDetailsArrayList.add(videoDetails);
In order to do that I have Generated API key and pasted into my project. Steps for generating API Key
Signup to google developer account using google mail
Click on create a project from dropdown icon arrow (YouTube API) at top of the page and type the project name like My YouTube Video App then click on Create button.
Go to API Manager from side menu and choose Registering your application under YouTube APIs and then click Enable API, then you will see a notice which saying Go to Credentials. Then, click that button.
The new page will appear like add credentials to your project and choose YouTube Data API v3 from which API are you using and choose Android as where will you be calling the API from. Finally choose Public data from dada accessing part and then click the button called What credentials do I need?
Then copy pasted my API key to my project.
Upvotes: 1
Views: 988
Reputation: 16354
From your LogCat
org.json.JSONException: No value for videoId
Seems like the JSON object doesn't contain any value/mapping for the parameter "videoId"
.
To check if the JSON contains a certain key, use has().
Or, you can use optString() to get an empty string if the mapping doesn't exist.
Upvotes: 1