在我的应用程序中集成youtube,如按钮和评论

时间:2017-03-02 11:38:24

标签: android android-youtube-api

在我的Android应用程序中,有一部分显示图片列表。每张图片都与视频ID相关联。点击该图片,相应的视频ID将传递给YouTube播放器,该视频将在YouTube播放器中播放。每张图片下方都有一个类似按钮和一个评论按钮。我想将它们与youtube一样整合和评论。所以,点击相似或评论我的应用中的视频与点击相似或评论youtube相同。我怎么能这样说呢?

1 个答案:

答案 0 :(得分:0)

将此代码用于youtube视频....

// getPostLikeBtn(创建此方法。)

private void getPostLikeBtn(final String rating) {
    String tag_json_obj = "recipeLike";


    final SpotsDialog spotsDialog = new SpotsDialog(context);

    spotsDialog.show();
    spotsDialog.setMessage("Loading...");

    StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST,
            "https://www.googleapis.com/youtube/v3/videos/rate",

            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    spotsDialog.dismiss();


                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            try {
                spotsDialog.dismiss();
                try {
                    if (error.networkResponse.data != null) {
                        try {
                            String body = new String(error.networkResponse.data, "UTF-8");
                            Log.e("errorLike", body);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }

                    spotsDialog.dismiss();
                    Toast.makeText(context, getResources().getString(R.string.try_again), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Toast.makeText(context, getResources().getString(R.string.try_again), Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
            // hide the progress dialog

        }
    }) {

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            int mStatusCode = response.statusCode;
            Log.e("mStatusCode", "" + mStatusCode);
            if (mStatusCode == 204) {
                Toast.makeText(context, "Successfully updated", Toast.LENGTH_SHORT).show();
                getLikeShare();

            } else {
                Toast.makeText(context, getResources().getString(R.string.try_again), Toast.LENGTH_SHORT).show();
            }
            return super.parseNetworkResponse(response);
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            SharedPreferences prefs = getSharedPreferences("GOOGLE_TOKEN", MODE_PRIVATE);
            String token = prefs.getString("token", null);
            params.put("Authorization", "Bearer " + token);
            return params;
        }

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> jsonObject = new HashMap<>();
            try {
                jsonObject.put("id", "Video id");
                jsonObject.put("rating", rating);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.e("jsonObject", "" + jsonObject);
            return jsonObject;
        }
    };


    AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);

}

我希望这对你有用..