honeysingh
honeysingh

Reputation: 23

networkOnMainThreadException error even with Async

I'm running the following code on Android Studio and trying to make an API call to the Semantics interface. This is my async code:

public class DownloadTest extends AsyncTask<Void, Void, Void> {

public String results = "hello";
public Products products = new Products("key",
        "password");

@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
    // Call your web service here
    Scan me = new Scan();

        /* Build the Request */
    products.productsField("upc", "883974958450").productsField("fields", "name", "gtins");

        /* Make the Request */
    try {
        results = products.getProducts().toString();
        Log.i("try", results);
        me.set_vars(results); //set_vars is a method from MainActivity.java
    } catch (Exception e) {
        Log.i("myTag0", e.toString());
    }
    return null;
}

}

I didn't include the code for onPreExecute() or onPostExecute() because I don't have anything in the code for those methods right now.

In the logcat for myTag0 I always see networkOnMainThreadException and I've looked at other threads and have still be unable to figure out why exactly I'm getting this error. I've included the correct Internet permissions in my AndroidManifest which are

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

In MainActivity.java, I make my call to the method doInBackground() the following way:

DownloadTest work = new DownloadTest();
    work.doInBackground();
    Log.i("mytag", my_result);

Thanks!

Edit: this is the error message/trace that I get when the app crashes if I change the statement above to work.execute()

07-13 13:25:17.824    2718-2733/com.example.jesarshah.snapcart E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.jesarshah.snapcart, PID: 2718
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)
 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.app.Activity.<init>(Activity.java:794)
        at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:76)
        at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:50)
        at android.support.v7.app.ActionBarActivity.<init>(ActionBarActivity.java:23)
        at com.example.jesarshah.snapcart.Scan.<init>(Scan.java:32)
        at com.example.jesarshah.snapcart.DownloadTest.doInBackground(DownloadTest.java:26)
        at com.example.jesarshah.snapcart.DownloadTest.doInBackground(DownloadTest.java:11)
        at android.os.AsyncTask$2.call(AsyncTask.java:292)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)             at java.lang.Thread.run(Thread.java:818)

Upvotes: 0

Views: 139

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006574

Do not call doInBackground(). Call execute() or executeOnExecutor() from the main application thread.

Since, in your case, you do not have onPostExecute() or any of the other typical AsyncTask methods, and since you are on a background thread when you want this work to be done, either:

  • Just do the work on that background thread that you are already on, and get rid of the AsyncTask entirely, or

  • Switch to a plain Thread

Also note that your Scan object appears to either be a subclass of ActionBarActivity or is creating an instance of ActionBarActivity. Neither of these are possible. NEVER create your own instances of activities.

Upvotes: 2

Related Questions