1

我想在用户点击完成后发送一个简单的 http 通知。我使用 AsyncTask 进行网络操作,但我不明白为什么应用程序仍然会因此异常而崩溃android.os.NetworkOnMainThreadException

private void buttonPressed() {
    EditText editTextLogin = (EditText) findViewById(R.id.editTextPassword);

    editTextLogin.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
            try{
                MyAsync test = new MyAsync(activity);
                test.execute("http://google.com");
            }catch (Exception e) {
                // TODO: handle exception
                Log.e("log_tag", "Error in http connection "+e.toString());
            }
            return true;
        }
    });
}

我不允许在该行运行此任务?

这是课程

class MyAsync extends AsyncTask<String, Void, String[]>{
    private final Activity activity;

    public MyAsync(Activity anActivity){
        activity = anActivity; //just in case i will need it later
    }

    @Override
    protected String[] doInBackground(String... args) {
        // TODO Auto-generated method stub
        Document doc = Jsoup.connect(args[0]).get();
        //do something with this document as soon this error android.os.NetworkOnMainThreadException wont rise again
        return new String[0];
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(String[] result) {
        Log.e('MyAsync','Finally i got it to work');
    }
}

错误日志猫:-

10-26 23:20:16.729: E/AndroidRuntime(1029): FATAL EXCEPTION: AsyncTask #1
10-26 23:20:16.729: E/AndroidRuntime(1029): java.lang.RuntimeException: An error occured while executing doInBackground()
10-26 23:20:16.729: E/AndroidRuntime(1029):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.lang.Thread.run(Thread.java:856)
10-26 23:20:16.729: E/AndroidRuntime(1029): Caused by: java.lang.VerifyError: org/jivesoftware/smack/sasl/SASLMechanism
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-26 23:20:16.729: E/AndroidRuntime(1029):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
4

1 回答 1

0

显然,您使用的是与 Android 不兼容的 Smack 版本。IIRC,您应该使用一个 aSmack 库。

您可以说这是 Smack 的问题,因为异常:

10-26 23:20:16.729: E/AndroidRuntime(1029): Caused by: java.lang.VerifyError: org/jivesoftware/smack/sasl/SASLMechanism

org.jivesoftware.smack是 Smack 库。

您还会注意到,这不是android.os.NetworkOnMainThreadException,而是java.lang.VerifyError

于 2012-10-27T07:40:00.407 回答