从Youtube API获取视频网址

时间:2015-12-13 14:08:07

标签: java json youtube-api

我有来自YouTube API的JSON。

以下是此link

中的JSON

我无法从中获取图像。我无法显示图片。

我在10天左右尝试,但我无法解决这个问题。任何人都可以解决这个问题

这是我的完整代码:

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();
    }
}

2 个答案:

答案 0 :(得分:3)

要从JSON获取图片网址,请使用以下代码: 在行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);

更新: 你会得到imgURL作为字符串; 例如:https://i.ytimg.com/vi/uU-uRElXM3A/default.jpg

我想你忘记将imageURL添加到&#34; map&#34;,

答案 1 :(得分:0)

要解析json,首先应该通过执行以下操作来定义JSONArray对象:

module.Orderline.extend({
  export_as_JSON: function() {
  debugger;
  return {
     something
  };
}

然后你需要遍历整个json数组来构建一个:

JSONArray jsonArray = new JSONArray(jsonArrayString);