Java 将 YouTube 数据 API 添加到 Android Studio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18175397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:02:28  来源:igfitidea点击:

Add YouTube Data API to Android Studio

javaandroidyoutube-apigradleandroid-studio

提问by Bigandrewgold

So, I am currently messing around with android programming in my free time, and I am using android studio as my ide of choice. I am currently trying to make a very simple app using the youtube api. My issue is that I cant figure out how to actually get the api into my application. I have used file>project structure to add the stuff to my application, but that doesnt work by itself. It stops yelling at me about syntax errors but when it compiles it errors.

所以,我目前在空闲时间忙于 android 编程,我使用 android studio 作为我的首选。我目前正在尝试使用 youtube api 制作一个非常简单的应用程序。我的问题是我无法弄清楚如何将 api 实际放入我的应用程序中。我已经使用文件> 项目结构将这些内容添加到我的应用程序中,但这本身并不起作用。它停止对我大喊语法错误,但是当它编译错误时。

I researched this a bit and have found that I need to add stuff to build.gradle or settings.gradle(or both) but i havent found a definitive answer on exactly what to do. Everything I have tried thus far hasnt worked. SO if someone could either explain to me what i have to do, or link me to a place where it explains what to do that would be great

我对此进行了一些研究,发现我需要向 build.gradle 或 settings.gradle(或两者)添加内容,但我还没有找到确切的答案。到目前为止我尝试过的一切都没有奏效。因此,如果有人可以向我解释我必须做什么,或者将我链接到一个可以解释该做什么的地方,那就太好了

--edit--

- 编辑 -

I am so confused right now. I started a new project. I added the libs and it was still able to compile. I then added the imports and it threw errors about it. I then added these to the build.gradle and it was able to compile perfectly

我现在很困惑。我开始了一个新项目。我添加了库,它仍然能够编译。然后我添加了导入,它抛出了关于它的错误。然后我将这些添加到 build.gradle 中,它能够完美编译

compile fileTree(dir: 'libs/youtube', include: '*.jar')
compile fileTree(dir: 'libs/youtube/libs', include: '*.jar')

But when i added this code that requires the library

但是当我添加这个需要库的代码时

/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = new HymansonFactory();

/** Global instance of Youtube object to make all API requests. */
private static YouTube youtube;

the syntax was fine but it threw an error upon compiling it

语法很好,但在编译时抛出了错误

 Gradle: Execution failed for task ':Apitest:dexDebug'.
> Failed to run command:

and that was followed by about 100 lines of of file locations

然后是大约 100 行文件位置

Any ideas as to what i am doing wrong?

关于我做错了什么的任何想法?

回答by Evgeni Roitburg

Just add this dependency in gradle file:

只需在 gradle 文件中添加此依赖项:

compile 'com.google.apis:google-api-services-youtube:v3-rev181-1.22.0'

and use YouTube object.

并使用 YouTube 对象。

reference: https://developers.google.com/api-client-library/java/apis/youtube/v3

参考:https: //developers.google.com/api-client-library/java/apis/youtube/v3

回答by Anand Kamathi

Step 1: Download YoutubeAndroidPlayerApi.jar from https://developers.google.com/youtube/android/player/downloads/

第 1 步:从https://developers.google.com/youtube/android/player/downloads/下载 YoutubeAndroidPlayerApi.jar

Step 2: Paste it in libs folder inside app folder of project

步骤 2:将其粘贴到项目 app 文件夹内的 libs 文件夹中

Step 3: Add following line to build.gradle in app folder of project:

第 3 步:在项目的 app 文件夹中的 build.gradle 中添加以下行:

compile files('libs/YouTubeAndroidPlayerApi.jar')

Step 4:Add following imports to activity which extends YouTubeBaseActivityand implements YouTubePlayer.OnInitializedListener

第 4 步:将以下导入添加到扩展YouTubeBaseActivity和实现的活动中YouTubePlayer.OnInitializedListener

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.Provider;

Step 5: Inside OnCreatemethod:

第 5 步:内部OnCreate方法:

//Make sure you initialize youtube player

YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

youTubeView.initialize(YoutubeAPIKey, this);

Where YouTubeAPIKeyyou would get from google console after you register your project in web mode, dont select mobile domain like Android or any

YouTubeAPIKey您将从您的Web模式寄存器项目后,谷歌控制台中看到,不选择像Android或任何移动领域

Step 6: Override methods in interface implemented as:

第 6 步:重写接口中的方法,实现为:

@Override
public void onInitializationFailure(Provider arg0,
        YouTubeInitializationResult error) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Oh no!      
    "+error.toString(),Toast.LENGTH_LONG).show();

}

@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer player,
        boolean arg2) {
    // TODO Auto-generated method stub
    player.loadVideo(VIDEO_ID);
}

Where VIDEO_ID is fetched from RESTful call to Youtube API at: "https://www.googleapis.com/youtube/v3/search" with suitable parameters passed in GET like, "q,relevanceLanguage,type,key" etc.

VIDEO_ID 是从 RESTful 调用 Youtube API 获取的:“ https://www.googleapis.com/youtube/v3/search”,并在 GET 中传递合适的参数,例如“q,relevanceLanguage,type,key”等。