Issue
I have an Android app where I am trying to get a response a REST call that I have into another class file in my Android app.
Here is the class file that makes the REST call:
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;
}
}
I am trying to pull the result into another class file as follows:
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("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipx9NPyfGTq8uYxEBlwmpC_LWCVpFWfLV0gn7xvXDRRC9-awRZe-2h3WLNDOdTfbp8aJTguxWMT6Q5_p9R_jBHDN7Cy9g3GVct-CULJwWOZ9xOMfq4pfny5DDGrPoJFvOk5qdzsUf5OSoc/s1600/%2528393%2529.jpg");
movie1.setyTubeID(YT_ID);
I get the following error when it runs:
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)
When I run it on a physical device - the spinner for the YT video just spins and spins and the video never seems to load and it does not return an error message at all in the app run window of Android Studio.
Where have I made an error, and what is the best way to fix this?
Solution
Explain
You're using AsyncTask
incorrectly.
AsyncTask
implements 4-step.
- onPreExecute
- doInBackground
- onProgressUpdate
- onPostExecute
If finished your doInBackground then call onPostExecute
.
Therefore, you have to handled using onPostExecute
.
You must check the reference. There is a detailed explanation.
Solution
implement interface for handling event.
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("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipx9NPyfGTq8uYxEBlwmpC_LWCVpFWfLV0gn7xvXDRRC9-awRZe-2h3WLNDOdTfbp8aJTguxWMT6Q5_p9R_jBHDN7Cy9g3GVct-CULJwWOZ9xOMfq4pfny5DDGrPoJFvOk5qdzsUf5OSoc/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;
}
}
Reference
https://developer.android.com/reference/android/os/AsyncTask.html
Answered By - Ethan Choi
Answer Checked By - Mary Flores (JavaFixing Volunteer)