我无法在Android应用中使用谷歌api java客户端YouTubeSample

时间:2010-11-22 17:10:49

标签: android google-api

我只想在android中获取我的视频playUrl .... 我已经检查了在Android中使用YouTubeSample的相关问题,他们的回答是肯定的 但是,当我在模拟器或我的Android设备中运行我的代码时,它不起作用 我的代码是:

    import java.io.IOException;
    import com.google.api.client.googleapis.GoogleHeaders;
    import com.google.api.client.googleapis.GoogleTransport;
    import com.google.api.client.googleapis.json.*;
    import com.google.api.client.http.*;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    public class main extends Activity {
     HttpTransport transport;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Debug.enableLogging();
            try {
              try {
                HttpTransport transport = setUpTransport();
                showVideos(transport);
              } catch (HttpResponseException e) {

                throw e;
              }
            } catch (Throwable t) {
              t.printStackTrace();

            }
        }
        private static HttpTransport setUpTransport() {
            HttpTransport transport = GoogleTransport.create();
            GoogleHeaders headers = (GoogleHeaders) transport.defaultHeaders;
            headers.setApplicationName("Google-YouTubeSample/1.0");
            headers.gdataVersion = "2";
            transport.addParser(new JsonCParser());
            return transport;
          }

          private static VideoFeed showVideos(HttpTransport transport)
              throws IOException {
            //View.header("Get Videos");
            // build URL for the video feed for "search stories"
            YouTubeUrl url = YouTubeUrl.forVideosFeed();
            url.author = "cecol3500123";
            // execute GData request for the feed
            VideoFeed feed = VideoFeed.executeGet(transport, url);
            //View.display(feed);
            return feed;
          }
    }

我编译好了,但是在设备上只是应用程序主服务器意外停止

我发现我总是停在代码上:HttpTransport transport = GoogleTransport.create();

AndroidManifest已设置权限:     <uses-permission android:name="android.permission.INTERNET"></uses-permission>

我的项目有 google api java客户端库。     我设定的,没关系吗?     我已经尝试了所有可能的(我已经一次又一次地修改了代码)。     我不知道该怎么做。     请帮帮我,我只想在android中获取我的视频playUrl ....     (或者有人可以给我正确的代码?)。     抱歉我的英语不好

renew with debug trace
this is my program Debug trace                             
11-23 09:45:10.466: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f0700e5                                 
11-23 09:45:10.486: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f020031        
11-23 09:45:10.496: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f020030        
11-23 09:45:10.506: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f050000       
11-23 09:45:10.716: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f060000            
11-23 09:45:10.856: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f060001                       
11-23 09:45:11.866: DEBUG/ddm-heap(180): Got feature list request    
11-23 09:45:12.166: DEBUG/ddm-heap(185): Got feature list request                
11-23 09:45:12.526: DEBUG/ddm-heap(165): Got feature list request               
11-23 09:45:12.716: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f0700e5                             
11-23 09:45:12.756: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f020031                                
11-23 09:45:12.767: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f020030                           
11-23 09:45:12.776: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f050000                       
11-23 09:45:12.796: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f060000                        
11-23 09:45:12.826: WARN/ResourceType(62): Resources don't contain package for resource number 0x7f060001     
11-23 09:45:13.286: DEBUG/ddm-heap(143): Got feature list request    
11-23 09:45:13.646: WARN/BackupManagerService(62): dataChanged but no participant pkg='com.android.providers.settings' uid=10017      
11-23 09:45:13.946: WARN/BackupManagerService(62): dataChanged but no participant pkg='com.android.providers.settings' uid=10017    
11-23 09:45:14.426: DEBUG/dalvikvm(62): GC freed 8344 objects / 558776 bytes in 461ms    
11-23 09:45:14.826: INFO/ActivityManager(62): Start proc com.android.email for broadcast     com.android.email/com.android.exchange.BootReceiver: pid=205 uid=10023 gids={3003, 1015}
11-23 09:45:14.836: INFO/ActivityManager(62): Starting activity: Intent {        act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.testcom/.main }        
11-23 09:45:15.066: DEBUG/AndroidRuntime(185): Shutting down VM          
11-23 09:45:15.076: DEBUG/dalvikvm(185): DestroyJavaVM waiting for non-daemon threads to exit           
11-23 09:45:15.106: DEBUG/dalvikvm(185): DestroyJavaVM shutting VM down    
11-23 09:45:15.106: DEBUG/dalvikvm(185): HeapWorker thread shutting down    
11-23 09:45:15.116: DEBUG/dalvikvm(185): HeapWorker thread has shut down     
11-23 09:45:15.127: DEBUG/jdwp(185): JDWP shutting down net...    
11-23 09:45:15.127: INFO/dalvikvm(185): Debugger has detached; object registry had 1 entries               
11-23 09:45:15.147: DEBUG/dalvikvm(185): VM cleaning up            
11-23 09:45:15.226: ERROR/AndroidRuntime(185): ERROR: thread attach failed         
11-23 09:45:15.496: DEBUG/dalvikvm(185): LinearAlloc 0x0 used 639500 of 5242880 (12%)          
11-23 09:45:15.566: WARN/ActivityManager(62): Activity pause timeout for     HistoryRecord{43cec138 com.android.launcher/.Launcher}     
11-23 09:45:15.676: INFO/ActivityManager(62): Start proc com.testcom for activity     com.testcom/.main: pid=210 uid=10027 gids={3003, 1015}   
11-23 09:45:16.266: DEBUG/ddm-heap(205): Got feature list request   
11-23 09:45:16.786: DEBUG/ddm-heap(210): Got feature list request   
11-23 09:45:16.976: DEBUG/dalvikvm(30): GC freed 284 objects / 10848 bytes in 1262ms   
11-23 09:45:17.517: DEBUG/dalvikvm(30): GC freed 50 objects / 2224 bytes in 410ms   
11-23 09:45:17.647: INFO/dalvikvm(210): Could not find method    com.google.api.client.googleapis.GoogleTransport.create, referenced from method com.testcom.main.setUpTransport     
11-23 09:45:17.657: WARN/dalvikvm(210): VFY: unable to resolve static method 4:     Lcom/google/api/client/googleapis/GoogleTransport;.create ()Lcom/google/api/client/http/HttpTransport;
11-23 09:45:17.657: DEBUG/dalvikvm(210): VFY: replacing opcode 0x71 at 0x0009    
11-23 09:45:17.677: DEBUG/dalvikvm(210): Making a copy of Lcom/testcom/main;.setUpTransport code (128 bytes)    
11-23 09:45:17.687: WARN/dalvikvm(210): VFY: unable to find class referenced in signature (Lcom/google/api/client/http/HttpTransport;)
11-23 09:45:17.697: WARN/dalvikvm(210): Unable to resolve superclass of Lcom/testcom/YouTubeUrl; (6)
11-23 09:45:17.777: WARN/dalvikvm(210): Link of class 'Lcom/testcom/YouTubeUrl;' failed   
11-23 09:45:17.777: INFO/dalvikvm(210): Could not find method     com.testcom.YouTubeUrl.forVideosFeed, referenced from method com.testcom.main.showVideos
11-23 09:45:17.787: WARN/dalvikvm(210): VFY: unable to resolve static method 40:     Lcom/testcom/YouTubeUrl;.forVideosFeed ()Lcom/testcom/YouTubeUrl;
11-23 09:45:17.787: DEBUG/dalvikvm(210): VFY: replacing opcode 0x71 at 0x0007    
11-23 09:45:17.826: DEBUG/dalvikvm(210): Making a copy of Lcom/testcom/main;.showVideos code (56 bytes)    
11-23 09:45:17.867: WARN/dalvikvm(210): VFY: unable to find class referenced in signature (Lcom/google/api/client/http/HttpTransport;)      
11-23 09:45:17.877: WARN/dalvikvm(210): VFY: unable to find class referenced in signature (Lcom/google/api/client/http/HttpTransport;)      
11-23 09:45:17.927: WARN/dalvikvm(210): VFY: unable to resolve exception class 13 (Lcom/google/api/client/http/HttpResponseException;)   
11-23 09:45:17.937: WARN/dalvikvm(210): VFY: unable to find exception handler at addr 0x13     
11-23 09:45:17.947: WARN/dalvikvm(210): VFY:  rejected Lcom/testcom/main;.onCreate (Landroid/os/Bundle;)V     
11-23 09:45:17.957: WARN/dalvikvm(210): VFY:  rejecting opcode 0x0d at 0x0013    
11-23 09:45:17.957: WARN/dalvikvm(210): VFY:  rejected Lcom/testcom/main;.onCreate     (Landroid/os/Bundle;)V
11-23 09:45:18.017: WARN/dalvikvm(210): Verifier rejected class Lcom/testcom/main;    
11-23 09:45:18.017: WARN/dalvikvm(210): Class init failed in newInstance call     (Lcom/testcom/main;)
11-23 09:45:18.027: DEBUG/AndroidRuntime(210): Shutting down VM     
11-23 09:45:18.037: WARN/dalvikvm(210): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-23 09:45:18.047: ERROR/AndroidRuntime(210): Uncaught handler: thread main exiting due to uncaught exception     
11-23 09:45:18.107: DEBUG/dalvikvm(30): GC freed 2 objects / 48 bytes in 568ms    
11-23 09:45:18.167: ERROR/AndroidRuntime(210): java.lang.VerifyError: com.testcom.main    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at    java.lang.Class.newInstanceImpl(Native Method)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at java.lang.Class.newInstance(Class.java:1479)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)   
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)   
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at android.os.Handler.dispatchMessage(Handler.java:99)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at android.os.Looper.loop(Looper.java:123)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at android.app.ActivityThread.main(ActivityThread.java:4363)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at java.lang.reflect.Method.invokeNative(Native Method)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at java.lang.reflect.Method.invoke(Method.java:521)       
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)    
11-23 09:45:18.167: ERROR/AndroidRuntime(210):     at dalvik.system.NativeStart.main(Native Method)    
11-23 09:45:18.237: INFO/ActivityThread(205): Publishing provider    com.android.email.provider: com.android.email.provider.EmailProvider   

3 个答案:

答案 0 :(得分:1)

与权限无关。

我怀疑发生的事情是您在编译类路径上有com.google.api.client.googleapis.GoogleTransport,但不是运行时类路径。或者您可能正在针对一个版本进行编译,但是针对该库的另一个版本运行。

这种情况发生在我身上,例如当我使用Maven时,由于某种原因,当通过Eclipse运行时,它不受尊重。您必须确保在.classpath中明确指定了对google-api-java-client软件包的依赖。

答案 1 :(得分:1)

嘿,大家(只有两个人) 我发现问题是我的库路径不对 编译类路径是正确的,但运行时类路径在eclipse中是错误的 虽然我不知道是怎么回事 我的解决方案是直接在项目下添加.jar文件而不是创建用户库(包括.jar文件)并将其添加到项目中 因此,youtubesample可以在android上运行,但你需要注意类路径设置

答案 2 :(得分:0)

您肯定会添加以下内容:

 <uses-library android:name="com.google.android.something" />
你的清单中的

(我不知道google transport的内容是什么)