0

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)

honeysingh
  • 23
  • 4

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This is the error message I get when I make it work.execute() . The app just crashes and can't open. [I pasted it below] – honeysingh Jul 13 '15 at 20:26
  • @honeysingh Pasted below where? – Psypher Jul 13 '15 at 20:28
  • Sorry I pasted it in the edits of my actual answer. My bad, new to stackoverflow! – honeysingh Jul 13 '15 at 20:29
  • @CommonsWare what would the onPostExecute() have to look like? Is that the reason why we're getting the error? – honeysingh Jul 13 '15 at 20:36
  • @CommonsWare so would it be best to just have a thread that's part of the code in the main activity? (based on what you said about not avoiding creating instances of activities) – honeysingh Jul 13 '15 at 20:41
  • @honeysingh: Sorry, but I do not know enough about your app to say what the best solution is. – CommonsWare Jul 13 '15 at 20:42
  • @honeysingh if your AsyncTask is an inner class of your activity, you can call your method directly: `set_vars(results);`. If it modifies the UI, it should be handled in the onPostExecute. If not, you can use a callback: http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui – tachyonflux Jul 13 '15 at 20:53