我有一个Android应用程序,我试图在我的Android应用程序中获取一个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;
}
}
我试图将结果拉到另一个类文件中,如下所示:
YouTubeVidIDs newID = new YouTubeVidIDs();
String YT_ID = newID.getCurrent_YTVidID();
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);
运行时出现以下错误:
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的应用运行窗口中根本不会返回错误消息。
有人可以帮我理解我在哪里犯了错误,解决这个问题的最佳方法是什么?
答案 0 :(得分:1)
<强>解释强>
您错误地使用了AsyncTask
。
AsyncTask
实现了4步。
如果完成了doInBackground,请拨打onPostExecute
。
因此,您必须使用onPostExecute
处理。
您必须检查参考。有一个详细的解释。
<强>解决方案强>
实现处理事件的接口。
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);
}
}
<强>的AsyncTask 强>
// 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;
}
}
<强>参考强>
https://developer.android.com/reference/android/os/AsyncTask.html
答案 1 :(得分:0)
返回的String current_YTVidID
被声明为类成员,在doInBackground()
中创建的类的实例在方法运行后发布,其中包含所有这些& #39;参数。
尝试在方法(doInBackground)中声明current_YTVidID
- 它应该将结果作为String返回,该String与处理它的实例无关。