Student Student - 1 month ago 11
Java Question

What's wrong with my async caller?

The parser is okay but when I put it into the context of asynctask it throws IOE exception. What should I do?

private class AsyncCaller extends AsyncTask<Void, Void, List<DummyContent.DummyItem>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// This method will be running on UI thread
}

@Override
protected List<DummyContent.DummyItem> doInBackground(Void... params) {
// This method will be running on background thread so don't update UI from here
// Do your long running http tasks here and you can access the parent class' variable url over here
List<DummyContent.DummyItem> list = new ArrayList<>();

Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
DummyContent.UNAME, DummyContent.PASSWORD.toCharArray());
}
});

try {
DummyContentRegistry registry = new XMLDummyContentParser(DummyContent.url).parse();
for (DummyContent.DummyItem t: registry.getTeachers()) {
list.add(t);
}
} catch (Exception e) {
e.printStackTrace();
fail("Exception should not have been thrown");
}

return list;
}

@Override
protected void onPostExecute(List<DummyContent.DummyItem> result) {
super.onPostExecute(result);
// This method will be running on UI thread
for (int i = 1; i <= DummyContent.COUNT; i++) {
DummyContent.addItem(DummyContent.createDummyItem(i));
}
for (DummyContent.DummyItem t: result) {
DummyContent.addItem(t);
}
}
}


meantime I've got stack trace:

08-07 21:34:43.264 26425-26775/ca.ubc.econ.montoya3.myapplication E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: ca.ubc.econ.montoya3.myapplication, PID: 26425
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.AssertionError: Exception should not have been thrown
at org.junit.Assert.fail(Assert.java:88)
at ca.ubc.econ.montoya3.myapplication.MainActivity$AsyncCaller.doInBackground(MainActivity.java:236)
at ca.ubc.econ.montoya3.myapplication.MainActivity$AsyncCaller.doInBackground(MainActivity.java:208)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 


What I want to do is to update a list fragment in tabbed activity? Without fail... I get

08-07 23:53:58.161 2567-3115/ca.ubc.econ.montoya3.myapplication W/System.err: java.io.IOException: Couldn't open https://oceanandcrow.ca/android/XML/Teachers
08-07 23:53:58.168 2567-3115/ca.ubc.econ.montoya3.myapplication W/System.err: Caused by: java.net.UnknownHostException: Unable to resolve host "oceanandcrow.ca": No address associated with hostname
08-07 23:53:58.169 2567-3115/ca.ubc.econ.montoya3.myapplication W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)

Answer

It not like what i thought before ,seems like it connection problem ,verify your internet status, internet permissions

Best practices :

  • Should implement Run-time permission request model for marshmallow and above (make sure to use a conditional check for Build version and use code accordingly )
  • Implement connectivity checks to verify the internet connectivity (use NetworkInfo with ConnectivityManager)
  • To avoid crash, Always use Thread for REST calls plus for long running tasks and use AsynchTasks for small tasks (downloading a song, image processing ,size around 5-10 MB) or use maturely developed libraries like Volley,Retrofit, (picasso,glide for images).
Comments