Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试从REST请求获取响应_Java_Android_Rest_Youtube Api - Fatal编程技术网

Java 正在尝试从REST请求获取响应

Java 正在尝试从REST请求获取响应,java,android,rest,youtube-api,Java,Android,Rest,Youtube Api,我有一个安卓应用程序,我试图得到一个REST调用的响应,我已经在安卓应用程序的另一个类文件中 下面是进行REST调用的类文件: public class YouTubeVidIDs extends AsyncTask<String, String, String> { public String current_YTVidID; @Override public String doInBackground(String... params) {

我有一个安卓应用程序,我试图得到一个REST调用的响应,我已经在安卓应用程序的另一个类文件中

下面是进行REST调用的类文件:

public class YouTubeVidIDs extends AsyncTask<String, String, String> {
    public String current_YTVidID;

    @Override
    public String doInBackground(String... params) {
        String subYTURL_0 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UCos52azQNBgW63_9uDJoPDA";
        String subYTURL_1 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCos52azQNBgW63_9uDJoPDA";
        String subYTURL_2 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCROKYPep-UuODNwyipe6JMw";
        String subYTURL_3 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCN_mfNT4sUszWKfi0Urjv-g";

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(subYTURL_0)
                .get()
                //.addHeader("accept", "*/*")
                .build();
        try {
            String response = client.newCall(request).execute().toString();
            current_YTVidID = response;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return current_YTVidID;
    }

    public void setCurrent_YTVidID(String current_YTVidID) {
        this.current_YTVidID = current_YTVidID;
    }

    public String getCurrent_YTVidID() {
        return this.current_YTVidID;
    }
}
我在运行时遇到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: software.blackstone.tvsunnahboxmenu, PID: 3514
                  java.lang.IllegalStateException: android.os.DeadObjectException
                      at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.K(SourceFile:236)
                      at fpm.b(SourceFile:2715)
                      at fot.k(SourceFile:15217)
                      at vma.run(SourceFile:1051)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: android.os.DeadObjectException
                      at android.os.BinderProxy.transactNative(Native Method)
                      at android.os.BinderProxy.transact(Binder.java:615)
                      at com.google.android.apps.youtube.embeddedplayer.service.service.jar.IApiPlayerService$Stub$Proxy.e(SourceFile:473)
                      at com.google.android.youtube.api.jar.client.RemoteEmbeddedPlayer.K(SourceFile:233)
                      at fpm.b(SourceFile:2715) 
                      at fot.k(SourceFile:15217) 
                      at vma.run(SourceFile:1051) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
当我在物理设备上运行它时,YT视频的微调器只是不断旋转,视频似乎从未加载,而且在Android Studio的应用程序运行窗口中根本不会返回错误消息


有人能帮助我理解我在哪里犯了错误,以及修复错误的最佳方法是什么吗?

返回的字符串
current\u YTVidID
被声明为类成员,该类的实例是为在
doInBackground()中执行工作而创建的
在方法运行后发布,该方法包括其所有参数


尝试在方法(doInBackground)内声明
current\u YTVidID
,该方法将返回与处理结果的实例无关的字符串结果。

解释

您不正确地使用了
AsyncTask

AsyncTask
实现了4个步骤

  • 预先执行
  • 背景
  • onProgressUpdate
  • onPostExecute
  • 如果完成了您的doInBackground,则调用
    onPostExecute

    因此,必须使用
    onPostExecute
    进行处理

    您必须检查引用。有详细的解释。

    解决方案

    实现用于处理事件的接口

    异步任务

    // Notice.
    // you don't need AsyncTask<String, String, String>
    // AsyncTask<[Params], [Progess], [Result]>
    // Recommend AsyncTask<Void, Void, String> for your source code.
    
    public class YouTubeVidIDs extends AsyncTask<String, String, String> {
        public String current_YTVidID;
        private IYoutubeTask handler; 
    
        public YouTubeVidIDs(IYoutubeTask handler)
        {
            this.handler = handler;
        }
    
        @Override
        public String doInBackground(String... params) {
            String subYTURL_0 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UCos52azQNBgW63_9uDJoPDA";
            String subYTURL_1 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCos52azQNBgW63_9uDJoPDA";
            String subYTURL_2 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCROKYPep-UuODNwyipe6JMw";
            String subYTURL_3 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCN_mfNT4sUszWKfi0Urjv-g";
    
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(subYTURL_0)
                    .get()
                    //.addHeader("accept", "*/*")
                    .build();
            try {
                String response = client.newCall(request).execute().toString();
                current_YTVidID = response;
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return current_YTVidID;
        }
    
        // implements onFinished method.
        protected void onPostExecute(String result) {
            handler.onFinished(result);
        }
    
        public void setCurrent_YTVidID(String current_YTVidID) {
            this.current_YTVidID = current_YTVidID;
        }
    
        public String getCurrent_YTVidID() {
            return this.current_YTVidID;
        }
    }
    
    //注意。
    //您不需要异步任务
    //异步任务
    //为您的源代码推荐AsyncTask。
    公共类YouTubeVidIDs扩展异步任务{
    公共字符串当前\u yVidId;
    私人IYoutubeTask处理程序;
    公共YouTubeVidIDs(IYoutubeTask处理程序)
    {
    this.handler=handler;
    }
    @凌驾
    公共字符串doInBackground(字符串…参数){
    字符串subYTURL_0=”https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UCos52azQNBgW63_9uDJoPDA";
    字符串subYTURL_1=”https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCos52azQNBgW63_9uDJoPDA";
    字符串subYTURL_2=”https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCROKYPep-UuODNwyipe6JMw”;
    字符串subYTURL_3=”https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCN_mfNT4sUszWKfi0Urjv-g”;
    OkHttpClient=新的OkHttpClient();
    Request Request=newrequest.Builder()
    .url(子目录url_0)
    .get()
    //.addHeader(“接受”、“*/*”)
    .build();
    试一试{
    字符串响应=client.newCall(request.execute().toString();
    当前_YTVidID=响应;
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    返回当前视频;
    }
    //实现onFinished方法。
    受保护的void onPostExecute(字符串结果){
    handler.onFinished(结果);
    }
    public void setCurrent\u YTVidID(字符串current\u YTVidID){
    this.current_YTVidID=current_YTVidID;
    }
    公共字符串getCurrent_YTVidID(){
    返回this.current_YTVidID;
    }
    }
    
    参考


    我按照你的建议做了,我的getter和setter现在显示出错误。我做错了什么(getter和setter与上面发布的格式相同)?删除getter和setter-它们不再是必需的。是否调用newId.execute(“”);所以这是我的背景works@alfasin:是的,我做了,但应用程序仍然出错。我现在如何从我刚刚更改的类文件中获取结果字符串(很抱歉问这么一个小问题)?很难猜测您现在遇到了哪个错误。请使用当前运行的代码、您得到的错误、完整的stacktrace(如果适用)以及触发错误的行更新问题。调用newID.excute()。
    public YourClass implements IYoutubeTask
    {
        //....
    
        public yourFunction()
        {
            YouTubeVidIDs newID = new YouTubeVidIDs(this);
    
            //excute asynctask
            newID.execute();
    
            // you can used to new YouTubeVidIDs().execute(); 
        }
    
        //....
    
        @Override
        public void onFinished(String YT_ID)
        {
            Movie movie1 = new Movie();
            movie1.setId(1);
            movie1.getId(1);
            movie1.setTitle("Masjid Al-Haram");
            movie1.setStudio("Mekkah, KSA");
            movie1.setDescription("Live Stream of Masjid Al-Haram");
            movie1.setCardImageUrl("http://3.bp.blogspot.com/-ZKjKucsPdzI/TudWC99CE_I/AAAAAAAAAD8/qvWdDtw5IW0/s1600/%2528393%2529.jpg");
            movie1.setyTubeID(YT_ID);
        }
    
        //...
    
        public interface IYoutubeTask
        {
            void onFinished(String YT_ID);
        }
    }
    
    // Notice.
    // you don't need AsyncTask<String, String, String>
    // AsyncTask<[Params], [Progess], [Result]>
    // Recommend AsyncTask<Void, Void, String> for your source code.
    
    public class YouTubeVidIDs extends AsyncTask<String, String, String> {
        public String current_YTVidID;
        private IYoutubeTask handler; 
    
        public YouTubeVidIDs(IYoutubeTask handler)
        {
            this.handler = handler;
        }
    
        @Override
        public String doInBackground(String... params) {
            String subYTURL_0 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UCos52azQNBgW63_9uDJoPDA";
            String subYTURL_1 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCos52azQNBgW63_9uDJoPDA";
            String subYTURL_2 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCROKYPep-UuODNwyipe6JMw";
            String subYTURL_3 = "https://sunnahboxservice.herokuapp.com/management/youtubeapi/liveStream?channelId=UUCN_mfNT4sUszWKfi0Urjv-g";
    
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(subYTURL_0)
                    .get()
                    //.addHeader("accept", "*/*")
                    .build();
            try {
                String response = client.newCall(request).execute().toString();
                current_YTVidID = response;
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return current_YTVidID;
        }
    
        // implements onFinished method.
        protected void onPostExecute(String result) {
            handler.onFinished(result);
        }
    
        public void setCurrent_YTVidID(String current_YTVidID) {
            this.current_YTVidID = current_YTVidID;
        }
    
        public String getCurrent_YTVidID() {
            return this.current_YTVidID;
        }
    }