Amit Saha
Amit Saha

Reputation: 121

Get video URL from Youtube API

I have a JSON from YouTube API.

Here is my JSON in this link

I am having trouble to get image from it. I unable to Show Image.

I am trying for it around 10days but i can't solve this.Can Anyone please solve this out

here is my full code:

public class MainActivity extends ActionBarActivity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String imgURL = "https://i.ytimg.com/vi/uU-uRElXM3A/default.jpg";

static String VIDEO_ID = "videoId";
static String TITLE = "title";
//static String DESCRIPTION = "description";
static String THUMBNAILS = "thumbnails";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
}

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Your Youtube Video is");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();

        // Retrieve JSON Objects from the given URL address



        jsonobject = JSONfunctions.getJSONfromURL("https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=natok+2015&maxResults=50&key=AIzaSyCojCp66RLS9OY8hOwnW0UWLNdC56z24Os");

        try {
            // Locate the array name in JSON
            JSONArray jsonarray = jsonobject.getJSONArray("items");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                JSONObject jsonObjId = jsonobject.getJSONObject("id");
                map.put("videoId", jsonObjId.getString("videoId"));

                JSONObject jsonObjSnippet = jsonobject.getJSONObject("snippet");
                 map.put("title", jsonObjSnippet.getString("title"));

                //JSONObject jsonObjThumbnail = jsonObjSnippet.getJSONObject("thumbnails");
               // String imgURL = jsonobject.getJSONObject("thumbnails").getString("url"); //use "medium/high" instead of default as required.
                JSONObject jsonObjThumbnail = jsonobject.getJSONObject("thumbnails");
                String imgURL = jsonobject.getJSONObject("default").getString("url");









                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(MainActivity.this, arraylist);
        // Set the adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}

Upvotes: 0

Views: 298

Answers (2)

Blue_Alien
Blue_Alien

Reputation: 2177

to get image URL from the JSON, please use the code: after the line JSONObject jsonObjSnippet = jsonobject.getJSONObject("snippet");

JSONObject jsonObjThumbnail = jsonobject.getJSONObject("thumbnails");
String imgURL = jsonObjThumbnail.getJSONObject("default").getString("url");//use "medium/high" instead of default as required.
map.put("url", imgURL);

UPDATE: you will get imgURL as String; Eg: https://i.ytimg.com/vi/uU-uRElXM3A/default.jpg

I think you forgot to add imageURL to "map",

Upvotes: 3

Yzgeav Zohar
Yzgeav Zohar

Reputation: 175

To parse your json you should first define JSONArray object by doing:

JSONArray jsonArray = new JSONArray(jsonArrayString);

Then you need to loop throughout your json array to build one:

JSONArray jsonArray = jsnobject.getJSONArray("array-key");
for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject newObj = jsonArray.getJSONObject(i);
}

Upvotes: 0

Related Questions