Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Youtube API获取视频URL_Java_Json_Youtube Api - Fatal编程技术网

Java 从Youtube API获取视频URL

Java 从Youtube API获取视频URL,java,json,youtube-api,Java,Json,Youtube Api,我有一个来自YouTube API的JSON 这是我的JSON 我很难从中获得图像。我无法显示图像 我试了大约10天,但我解决不了这个问题。有人能解决这个问题吗 这是我的全部代码: public class MainActivity extends ActionBarActivity { // Declare Variables JSONObject jsonobject; JSONArray jsonarray; ListView listview; ListViewAdapter adapt

我有一个来自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();
    }
}
公共类MainActivity扩展了ActionBarActivity{
//声明变量
JSONObject JSONObject;
JSONArray JSONArray;
列表视图列表视图;
ListViewAdapter适配器;
进程对话框;
ArrayList ArrayList;
静态字符串imgURL=”https://i.ytimg.com/vi/uU-uRElXM3A/default.jpg";
静态字符串VIDEO_ID=“videoId”;
静态字符串TITLE=“TITLE”;
//静态字符串DESCRIPTION=“DESCRIPTION”;
静态字符串缩略图=“缩略图”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//从listview_main.xml获取视图
setContentView(R.layout.listview_main);
//执行下载JSON异步任务
新建下载JSON().execute();
}
//下载JSON异步任务
私有类下载JSON扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//创建一个progressdialog
mProgressDialog=新建ProgressDialog(MainActivity.this);
//设置进程对话框标题
setTitle(“你的Youtube视频是”);
//设置进程对话框消息
设置消息(“加载…”);
mProgressDialog.setUndeterminate(false);
//显示进度对话框
mProgressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//创建一个数组
arraylist=新的arraylist();
//从给定的URL地址检索JSON对象
jsonobject=JSONfunctions.getJSONfromURL(“https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=natok+2015年&maxResults=50&key=AizasycojCP66RLS9OY8OWW0UWLNDC56Z24OS”);
试一试{
//在JSON中找到数组名称
JSONArray JSONArray=jsonobject.getJSONArray(“项”);
for(int i=0;i
要解析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);
}
JSONArray-JSONArray=jsnobject.getJSONArray(“数组键”);
for(int i=0;i
要解析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);
}
JSONArray-JSONArray=jsnobject.getJSONArray(“数组键”);
for(int i=0;i
要从JSON获取图像URL,请使用以下代码: 在行
JSONObject之后,jsonobjectsnippet=JSONObject.getJSONObject(“snippet”);

更新:
您将得到imgURL作为字符串; 例如:


我想您忘了将imageURL添加到“map”中,

要从JSON获取图像URL,请使用以下代码: 在行
JSONObject之后,jsonobjectsnippet=JSONObject.getJSONObject(“snippet”);

更新:
您将得到imgURL作为字符串; 例如:


我想你忘了在“地图”中添加imageURL,

说图像我想你是指缩略图,所以如果你在解析json,你所要做的就是进入items数组,然后是snippet,然后是缩略图。你能证明我的一个例子吗?说图像我想你是指缩略图,所以如果你在解析json,你所要做的就是进入items数组,然后是Through你能为我证明一个样本吗?可能重复的请看更新的anser,它会帮助你,肯定可能重复的请看更新的anser,它会帮助你,肯定imgURL的意思是什么?imgURLi的值是多少?Find 12-14 17:56:27.913 6946-6966/amit.jsonparsewithimage e/错误:没有缩略图的值这个错误你会得到imgURL作为字符串;例如:你能发布堆栈跟踪吗?你现在面临什么问题?在我之前的评论中,我发布了imgURL的意思是什么&imgURLi find的值是什么12-14 17:56:27.913 6946-6966/amit.jsonparsewithimage E/错误:缩略图没有值这个错误你会得到imgURL作为字符串;例如:你能发布堆栈跟踪?您现在面临什么问题?在我之前的评论中,我发布了它