Volley trying to use Youtube Data API - android

i have a problem trying to retrieve some date from googleapis, youtubeapi.
here is the link:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUpXSJOEKMoOhdwdh_7AJ2Vg&key=SOMEKEY
If i go to any browser and paste that url i have all the data that i need, a json like this:
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"ld9biNPKjAjgjV7EZ4EKeEGrhao/CokfSR1Ka3d1R0DGLPIfTD3IZqU\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 993,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"ld9biNPKjAjgjV7EZ4EKeEGrhao/hYDAQqaQpnjwln2FQWxqH4pNDwM\"",
"id": "VVVwWFNKT0VLTW9PaGR3ZGhfN0FKMlZnLk1OMXp4Vm5WWEZv",
"snippet": {
"publishedAt": "2017-11-30T13:53:50.000Z",
"channelId": "UCpXSJOEKMoOhdwdh_7AJ2Vg",
"title": "Amaneciendo Con Dios - Celebrando La Vida Pastor General",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/MN1zxVnVXFo/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/MN1zxVnVXFo/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/MN1zxVnVXFo/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "TaberTV",
"playlistId": "UUpXSJOEKMoOhdwdh_7AJ2Vg",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "MN1zxVnVXFo"
}
}
},`
}
And the json goes on.
When i try to retrieve it with volley i get this error:
12-01 10:54:08.082 3690-3690/com.tabernaculo.app E/Volley: [1] 4.onErrorResponse: Error:
My code to retrieve data with volley works with other websites, how can i make it work with youtube? This is how i send the url
urlEncoded = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId="+URLEncoder.encode(playListID,"UTF-8").toString()+"&key="+URLEncoder.encode(APIKEY,"UTF-8").toString();
I have tried pasting just the parameters without URLEncoder and there's no change.

Related

How to get Image URL based on name

My JSON contains only name of the food categories, Looks like below
[
{
"cu_id": "2",
"cuisine_type": "American"
},
{
"cu_id": "24",
"cuisine_type": "Sandwich"
},
{
"cu_id": "17",
"cuisine_type": "Seafood"
},
{
"cu_id": "29",
"cuisine_type": "Sports Bar"
},
{
"cu_id": "5",
"cuisine_type": "Steak"
},
{
"cu_id": "20",
"cuisine_type": "Sushi"
}
]
I want to get default image automatically from Internet by searching 'cuisine_type', How to do that?
You can use pixabay api it's a free RESTful api for searching and retrieving free images and videos released on Pixabay under Creative Commons CC0.
Sending a GET request like this :
https://pixabay.com/api/?key={ KEY }&q=query&image_type=photo
where :
Key : the api key you get after creating an account
q : the queried object you need to look for
image_type : what kind of data you need (picture, video)
returned data will be something like this :
{
"total": 4692,
"totalHits": 500,
"hits": [
{
"id": 195893,
"pageURL": "https://pixabay.com/en/blossom-bloom-flower-yellow-close-195893/",
"type": "photo",
"tags": "blossom, bloom, flower",
"previewURL": "https://static.pixabay.com/photo/2013/10/15/09/12/flower-195893_150.jpg"
"previewWidth": 150,
"previewHeight": 84,
"webformatURL": "https://pixabay.com/get/35bbf209db8dc9f2fa36746403097ae226b796b9e13e39d2_640.jpg",
"webformatWidth": 640,
"webformatHeight": 360,
"imageWidth": 4000,
"imageHeight": 2250,
"imageSize": 4731420,
"views": 7671,
"downloads": 6439,
"favorites": 1,
"likes": 5,
"comments": 2,
"user_id": 48777,
"user": "Josch13",
"userImageURL": "https://static.pixabay.com/user/2013/11/05/02-10-23-764_250x250.jpg",
},
{
"id": 14724,
...
},
...
]
}
for more detail visit this link

How can I populate a recycler card view in Android from an API response

I have a unique response from an http GET request that I want to use to populate an android recycler view (using cardView).
Here is my response:
{
"kind": "youtube#searchListResponse",
"etag": "\"VPWTmrH7dFmi4s1RqrK4tLejnRI/tKvRclTeFduIQC9knqZs2CVfs7A\"",
"nextPageToken": "CAEQAA",
"regionCode": "EG",
"pageInfo": {
"totalResults": 12,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"VPWTmrH7dFmi4s1RqrK4tLejnRI/faNNEg16szqlc3xWy6X39iKKx7g\"",
"id": {
"kind": "youtube#video",
"videoId": "bP2P0oqoX4U"
},
"snippet": {
"publishedAt": "2014-01-10T18:18:55.000Z",
"channelId": "UCbvwLzU45wgAHOqXRQoC11A",
"title": "تعرف على برنارد لويس صاحب مشروع تقسيم العالم العربي والإسلامي",
"description": "مقطع من خطبة: (الجمعة 9 من ربيع الأول 1435هـ الموافق 10-1-2014م), والتي هي بعنوان: (ماذا لو تخلى الشعب عن الجيش؟) الراب...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/bP2P0oqoX4U/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/bP2P0oqoX4U/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/bP2P0oqoX4U/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "موقع الشيخ محمد سعيد رسلان",
"liveBroadcastContent": "none"
}
}
]
}
I am kind of stumped where to start. I was thinking make a POJO from the JSON response.
Can someone direct to what is best software design practice on this with some example code?
Thanks.
Make a collection of POJO using this response. And then pass that collection object to the Recyclerview adapter.
You can use this to generate POJO class.

Which Tag in Json file to fetch the YouTube Video on Android application on ListView Item?

Here is my json file i use thumbnail on listview but there no such tag that fetch videos . Define it which tag i use ?
That perfectly add thumbnail or other things
Below is my preview of Listview items
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"GM4ZnRh2gk1X1BLWgHklTm-3cgQ/uvcwjZ_9_NYCLF7JAzfY5FiuzuQ\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 1998,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#playlistItem",
"etag": "\"GM4ZnRh2gk1X1BLWgHklTm-3cgQ/dCBViPWfe-MGrqHIcuMZ55HcLJ0\"",
"id": "VVVHaENWR1owWlNwZTVoSkhXeWlMd0hBLlZHRHFaakFMSF9N",
"snippet": {
"publishedAt": "2017-01-26T08:58:26.000Z",
"channelId": "UCGhCVGZ0ZSpe5hJHWyiLwHA",
"title": "Top 10 - Scariest scenes in Resident evil 7",
"description": "Resident Evil 7 brings the series back to its roots while mak Epidemic Sound.\nhttp://www.epidemicsound.com/youtube-creator-subscription/",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/VGDqZjALH_M/default.jpg",
"width": 120,
"height": 90
},
},
"channelTitle": "ZoominGames",
"playlistId": "UUGhCVGZ0ZSpe5hJHWyiLwHA",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "VGDqZjALH_M"
}
}
},
Well, it is true that you cannot find any url in the json output. The only thing that you can get from here is the videoId.
You can use this videoId as part of this https://www.youtube.com/watch?v="insert_videoId" to get a video link.
So in your case, this is the link https://www.youtube.com/watch?v=VGDqZjALH_M

converting json response (from wordpress REST API version-2 Beta) to json array in Android Studio

Below is the json response from wordpress:
[{
"id": 711,
"date": "2015-09-28T01:00:03",
"date_gmt": "2015-09-27T19:30:03",
"guid": {
"rendered": "http:\/\/www.realcake.in\/?p=711"
},
"modified": "2015-09-28T01:51:52",
"modified_gmt": "2015-09-27T20:21:52",
"slug": "why-only-and-only-realcake",
"type": "post",
"link": "http:\/\/www.realcake.in\/why-only-and-only-realcake\/",
"title": {
"rendered": "Why only and only RealCake"
},
"content": {
"rendered": "<p><img class=\"size-full wp-image-725 aligncenter\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg\" alt=\"REALCAKE BAKERY IN GURGAON R.\" width=\"940\" height=\"225\" srcset=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg 940w,
},
"excerpt": {
"rendered": ""
},
"author": 4,
"featured_media": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"categories": [37, 35, 1],
"tags": [],
"_links": {
"self": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/711"
}],
"collection": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
}],
"about": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
}]
}
}, {
"id": 420,
"date": "2015-01-01T18:00:07",
"date_gmt": "2015-01-01T12:30:07",
"guid": {
"rendered": "http:\/\/www.realcake.in\/?p=420"
},
"modified": "2015-08-04T23:29:55",
"modified_gmt": "2015-08-04T17:59:55",
"slug": "why-choose-muffins-cakes-realcake",
"type": "post",
"link": "http:\/\/www.realcake.in\/why-choose-muffins-cakes-realcake\/",
"title": {
"rendered": "Why Choose Muffins & Cakes from RealCake Bakery?"
},
"content": {
"rendered": "<div id=\"attachment_423\" style=\"width: 559px\" class=\"wp-caption aligncenter\"><img class=\" wp-image-423\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/03\/Regular-muffins-e1425386051181-300x148.png\"
},
"excerpt": {
"rendered": "<p>. Feel free to write to us or call us if […]<\/p>\n"
},
"author": 4,
"featured_media": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"categories": [35],
"tags": [44, 41, 46, 40, 45, 42, 38, 39, 43],
"_links": {
"self": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/420"
}],
"collection": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
}],
"about": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
}]
}
}]
==>> I am using the code below to get JSONArray:
Response response = client.newCall(request).execute();
//here I am getting the json response(mentioned above) from wordpress rest api
String json = response.body().string();
JSONObject object = new JSONObject(json);
//Below code is giving me null array as I dont have "Array name" in my json response.
JSONArray array = object.optJSONArray("Name of Array in json response");
Please help me get JSONArray out of json response.
Looking at your JSON string, it seems that the entire response body is a JSON-Array, so you can simply parse it directly into a instance of JSONArray.
This is how you can do it:
//get your JSON body-string like you do
String json = response.body().string();
//parse it directly into JSONArray
JSONArray array = new JSONArray(json);
//do whatever you want with the array...
I hope this helps you.

skipping null values when parsing json in android?

I am parsing json object from facebook.
In my facebook json object, there are string keys inside key array "data".
My Code as follows,
"data": [
{
"id": "100001211447563_300696056647440",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee is now using Facebook in English (US).",
"story_tags": {
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-19T09:13:04+0000",
"updated_time": "2012-01-19T09:13:04+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298802933503419",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee started using Graph API Explorer.",
"story_tags": {
"23": [
{
"id": 145634995501895,
"name": "Graph API Explorer",
"offset": 23,
"length": 18
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"picture": "http://photos-a.ak.fbcdn.net/photos-ak-snc1/v43/11/145634995501895/app_1_145634995501895_4870.gif",
"link": "http://developers.facebook.com/tools/explorer/",
"caption": "A tool to help you browse objects within the Facebook Graph API, manage permissions, obtain access tokens and generally learn how it all works.",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
},
{
"name": "Like",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
}
],
"type": "link",
"created_time": "2012-01-16T09:47:57+0000",
"updated_time": "2012-01-16T09:47:57+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298789650171414",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee likes myFBCovers.com.",
"story_tags": {
"15": [
{
"id": 160901873998019,
"name": "myFBCovers.com",
"offset": 15,
"length": 14
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-16T08:57:33+0000",
"updated_time": "2012-01-16T08:57:33+0000",
"comments": {
"count": 0
}
}
when I parsing this json object, String key "likes" is sometimes exist and sometimes do not exist in "data" jsonArray. So when I parsing JsonString "likes", sometimes I gets JSONException since I am parsing null value. Is there any way to skipping "null" json value?
Please help me regarding this.
if try catch it, next processes will be skipped.
you can check by isNull("name") method.
Edit: this is to check key. since you mentioned key exists sometimes. did you mean the value?
use JSONObjects method has(key)
data = (JSONObject) new JSONTokener(jsonObjRecv.toString()).nextValue();
if data.has("like"){
...
}
to check whether the value is null check with isNull("name")

Categories

Resources