即使使用Async,networkOnMainThreadException也会出错

时间:2015-07-13 20:20:50

标签: java android android-asynctask

我在Android Studio上运行以下代码并尝试对Semantics接口进行API调用。这是我的异步代码:

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;
}

}

我没有包含onPreExecute()或onPostExecute()的代码,因为我现在的代码中没有任何内容。

在myTag0的logcat中,我总是看到networkOnMainThreadException,我已经查看了其他线程,仍然无法弄清楚为什么我收到此错误。我在AndroidManifest中包含了正确的Internet权限,这些权限是

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

在MainActivity.java中,我通过以下方式调用方法doInBackground():

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

谢谢!

编辑:如果我将上面的语句更改为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)

在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) 在java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:2)

请勿致电doInBackground()。从主申请主题中调用execute()executeOnExecutor()

因为在您的情况下,您没有onPostExecute()或任何其他典型的AsyncTask方法,并且因为您希望完成此项工作,所以您处于后台线程中,或者:

  • 只需在您已经使用的后台线程上完成工作,然后完全摆脱AsyncTask

  • 切换到普通Thread

另请注意,您的Scan对象似乎是ActionBarActivity的子类,或者正在创建ActionBarActivity的实例。这些都不可能。 从不创建自己的活动实例。