如何在 JAVA 中解析 YOUTUBE JSON 并在列表中显示 视图



如何在JAVA中解析JSON YouTube数据API v3?

这是我的json数据

我想在列表视图中显示数据&点击后将播放视频。我该怎么做?公共类MainActivity扩展ListActivity{

private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResult=30&q=natok+bangla+mosharrof+karim&key=AIzaSyCR40QlsuX0aFfBV-wEPDsH_jxna1tDFRA";
private static final String TAG_ITEMS = "items";
private static final String TAG_ID = "id";
private static final String TAG_ID_VIDEOID = "videoId";
private static final String TAG_TITLE = "title";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_THUMBANAILS_URL = "https://i.ytimg.com/vi/hlaX2OZ_kDg/default.jpg";
private static final String TAG_CHANNELTITLE = "channelTitle";
JSONArray items = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> itemsList;
ListView lv = getListView();
// Hashmap for ListView
ArrayList<HashMap<String, String>> itemslist;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    itemslist = new ArrayList<HashMap<String, String>>();
    ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String cost = ((TextView) view.findViewById(R.id.email))
                    .getText().toString();
            String description = ((TextView) view.findViewById(R.id.mobile))
                    .getText().toString();
            // Starting single contact activity
            Intent in = new Intent(getApplicationContext(),
                    SingleContactActivity.class);
            in.putExtra(TAG_ID, name);
            in.putExtra(TAG_TITLE, cost);
            in.putExtra(TAG_DESCRIPTION, description);
            startActivity(in);
        }
    });
    new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                // Getting JSON Array node
                items = jsonObj.getJSONArray(TAG_ITEMS);
                // looping through All Contacts
                for (int i = 0; i < items.length(); i++) {
                    JSONObject c = items.getJSONObject(i);
                    String id = c.getString(TAG_ID);
                    String videoId = c.getString(TAG_ID_VIDEOID);
                    String channelTitle = c.getString(TAG_CHANNELTITLE);
                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    contact.put(TAG_ID, id);
                    contact.put(TAG_ID_VIDEOID, videoId);
                    contact.put(TAG_CHANNELTITLE, channelTitle);

                    // adding contact to contact list
                    itemsList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, itemsList,R.layout.list_item, new String[] { TAG_TITLE, TAG_DESCRIPTION,
                TAG_CHANNELTITLE }, new int[] { R.id.name,
                R.id.email, R.id.mobile });
        setListAdapter(adapter);
    }
}

}

这是我的java代码

YouTube Data API文档为您想要做的事情提供了足够的样本和解释。

例如,search.list如果成功,将返回一个JSONObject,其子项为JSONArray。您可以获得这个JSONArray并将其传递给您的适配器。

有关如何开始使用YouTube Data API的更多信息,请查看"开始使用"页面。

相关内容

  • 没有找到相关文章

最新更新