从Youtube API获取视频URL



我有一个来自YouTube API的JSON。

这是我在这个链接中的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();
    }
}

要从JSON中获取图像URL,请使用以下代码:线路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,

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

JSONArray jsonArray = new JSONArray(jsonArrayString);

然后,您需要在整个json数组中循环以构建一个:

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

相关内容

  • 没有找到相关文章

最新更新