首页 \ 问答 \ 在Android上使用适用于Youtube的Google API Java客户端访问Android服务中的YouTube数据(Accesing youtube data from Android Service using Google API Java Client for Youtube on Android)

在Android上使用适用于Youtube的Google API Java客户端访问Android服务中的YouTube数据(Accesing youtube data from Android Service using Google API Java Client for Youtube on Android)

我在网上搜索了一些关于如何在Android服务中使用Google API Java客户端Youtube相关API for Android的任何信息,但还没有找到任何信息......

我已经在我的第一个Android应用中使用API​​来访问youtube数据,但这些都来自于活动内部。

我正在尝试了解是否以及如何从Android服务中做同样的事情,因为我正在尝试编写Android服务以在后台获取youtube搜索/播放列表/其他数据。 我不需要显示它,只需从我的服务中将信息存储在我的数据库中。 谁能指点我这方面的信息?

我是Android,Google API,Auth,网络服务访问等的新手(这是我的第一个应用程序并且第一次完成所有这些)....我仍在通过Google API Java客户端Android Java文档查看是否我可以找到任何东西

谢谢!


I've searched a bunch online for any info about how to use the Google API Java Client Youtube related APIs for Android from within an Android service but haven't found any info yet...

I'm already using the APIs in my first Android app to access youtube data but it is all from within Activities.

I'm trying to understand if and how I could do the same from an Android service since I'm trying to write an Android service to fetch youtube search / playlist / other data in the background. I don't need to display it, just store the info in my DB from within my service. Can anyone point me to info on this ?

I'm new to Android, Google APIs, Auth, web-services access etc (this is my first app & first time doing all of this)....am still going though the Google API Java Client Android Java docs to see if I can find anything there

Thanks!


原文:https://stackoverflow.com/questions/21489918
更新时间:2023-03-14 21:03

最满意答案

通常,重用现有功能接口而不是创建新接口更好。 在您的情况下, BinaryOperator<String>是您所需要的。 最好根据变量的含义命名变量,而不是按类型命名变量。 因此,你可能有:

public class SomeClass {
    private BinaryOperator<String> splitAtLastOccurence = 
        (s, d) -> s.substring(s.lastIndexOf(d)+1);
}

请注意,您可以简化单语句lambda,删除return关键字和大括号。 它可以像这样应用:

String valueAfterSplit = splitAtLastOccurence.apply(plainText, "=");

通常,如果您的类始终使用相同的函数,则无需将其存储在变量中。 改为使用普通旧方法:

protected static String splitAtLastOccurence(String s, String d) {
    return s.substring(s.lastIndexOf(d)+1);
}

然后打电话给它:

String valueAfterSplit = splitAtLastOccurence(plainText, "=");

当函数对另一个类或方法进行参数化时 ,函数很好,因此它可以用于不同的函数。 例如,您正在编写一些通用代码,可以使用其他other字符串处理字符串列表:

void processList(List<String> list, String other, BinaryOperator<String> op) {
    for(int i=0; i<list.size(); i++) {
        list.set(i, op.apply(list.get(i), other));
    }
}

或者更多java-8风格:

void processList(List<String> list, String other, BinaryOperator<String> op) {
    list.replaceAll(s -> op.apply(s, other));
}

通过这种方式,您可以将此方法用于不同的功能。 如果您已经如上所述定义了splitAtLastOccurence静态方法,则可以使用方法引用重用它:

processList(myList, "=", MyClass::splitAtLastOccurence);

Usually it's better to reuse existing functional interfaces instead of creating new ones. In your case the BinaryOperator<String> is what you need. And it's better to name the variables by their meaning, not by their type. Thus you may have:

public class SomeClass {
    private BinaryOperator<String> splitAtLastOccurence = 
        (s, d) -> s.substring(s.lastIndexOf(d)+1);
}

Note that you can simplify single-statement lambda removing the return keyword and curly brackets. It can be applied like this:

String valueAfterSplit = splitAtLastOccurence.apply(plainText, "=");

Usually if your class uses the same function always, you don't need to store it in the variable. Use plain old method instead:

protected static String splitAtLastOccurence(String s, String d) {
    return s.substring(s.lastIndexOf(d)+1);
}

And just call it:

String valueAfterSplit = splitAtLastOccurence(plainText, "=");

Functions are good when another class or method is parameterized by function, so it can be used with different functions. For example, you are writing some generic code which can process list of strings with additional other string:

void processList(List<String> list, String other, BinaryOperator<String> op) {
    for(int i=0; i<list.size(); i++) {
        list.set(i, op.apply(list.get(i), other));
    }
}

Or more in java-8 style:

void processList(List<String> list, String other, BinaryOperator<String> op) {
    list.replaceAll(s -> op.apply(s, other));
}

In this way you can use this method with different functions. If you already have splitAtLastOccurence static method defined as above, you can reuse it using a method reference:

processList(myList, "=", MyClass::splitAtLastOccurence);

相关问答

更多
  • 通常,重用现有功能接口而不是创建新接口更好。 在您的情况下, BinaryOperator是您所需要的。 最好根据变量的含义命名变量,而不是按类型命名变量。 因此,你可能有: public class SomeClass { private BinaryOperator splitAtLastOccurence = (s, d) -> s.substring(s.lastIndexOf(d)+1); } 请注意,您可以简化单语句lambda,删除r ...
  • @FunctionalInterface注解对于您的代码的编译时间检查很有用。 除了使用@FunctionalInterface中的Object中的方法或用作功能界面的任何其他接口的static , default和抽象方法之外,不能有多种方法。 但是,您可以使用没有此注释的lambdas,也可以覆盖没有@Override注释的方法。 从文档 一个功能界面只有一个抽象方法。 由于默认方法有一个实现,它们不是抽象的。 如果一个接口声明一个覆盖java.lang.Object的一个公共方法的抽象方法,那么它也不 ...
  • 指定lambda参数的类型( F ) fooMapping.put("name", (String someString) -> someString.length()); fooMapping.put("flavor", (Integer someInt) -> someInt + 1); 返回类型( T )将从lambda body表达式的类型推断出来。 Specify the type of the lambda parameter (F) fooMapping.put("name", (Strin ...
  • 您的Skip界面只有一个抽象方法( default和static方法不计算) - 从Sprint接口继承的sprint方法。 因此它是一个功能界面。 Your Skip interface has only one abstract method (default and static methods don't count) - the sprint method inherited from the Sprint interface. Therefore it is a functional inter ...
  • 如果你使用eclipse,请尝试: 窗口> Prefferences> Java>编辑器>保存操作 选中“在保存时执行所选操作”,“其他操作”并单击“配置”。 使用eclipse的Save Actions在真实生活编码中可能非常有用,但您可能会通过Save Actions向导学习一些整洁的java技巧。 Java是一种面向对象的语言。 你需要利用这个事实。 使用类将您的代码分隔成不同的逻辑/结构组件。 了解如何使用OOP。 遵循SOLID设计和使用设计模式 。 另一个重要的事情是要知道你的语言 。 首先阅读 ...
  • 显然,你可以跳过使用这些新界面,并用更好的名字推出自己的界面。 但有一些考虑因素: 除非您的自定义界面扩展了其中一个内置插件,否则您将无法在其他某个JDK API中使用自定义界面。 如果你总是用自己的方式滚动,在某些时候你会遇到一个你不能想到一个好名字的情况。 例如,我认为CheckPerson并不是一个真正意义上的好名字,尽管这是主观的。 大多数内置接口还定义了其他一些API。 例如, Predicate定义or(Predicate) and(Predicate)和negate() 。 Function定 ...
  • 你真的有几个选择! 您可以选择使用共享的Kohana模块进入“2项目”路线 - 但我个人不喜欢这种方法。 我个人会使用类似的方法作为多语言网站 - 所以...... apache(或其他)会重写m.example.tld / my / page - > www.example.tld / mobile / my / page 假设您使用Kohana3 - 标准路线可以更改为: Route::set('messages', '/((/(/)', ...
  • 当然,这是高度依赖于实现的,不建议用于生产代码,但对于一次性转换任务,它将适用于普通的Reflection操作和HotSpot / OpenJDK的当前实现: public class StaticArgumentsTest { static Function staticConsumer(String testValue) { return (st) -> testValue + " and " + st; } static fi ...
  • 来自JLS§9.8 (突出我的): 功能接口是一个只有一个抽象方法的接口( 除了Object的方法 ) 理由是 要使用该接口,您必须实例化它。 每个实例化都必然从Object继承,因此无论如何都要实现这些抽象方法。 另一种方法 - boolean equals(Object) - 是一个抽象方法的显式声明,否则将被隐式声明,并将由实现该接口的每个类自动实现。 作为一个功能接口,您不太可能想要调用Object定义的方法。 因此,在搜索要调用的方法时,这些方法不计算(因为可以在不命名该方法的情况下调用功能接口的 ...
  • List list = roster.parallelStream().filter((p) -> p.getAge() > 18).collect(Collectors.toList()); 如果我想重用过滤器逻辑,例如在Person中有一个方法“isAdult”,该怎么办? List list = roster.parallelStream().filter(Person::isAdult).collect(Collectors.toList()); 要么 List< ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的