1

I want to send a simple http notification after user hit DONE. I use AsyncTask for network operations and i dont understand why application still crash with this exception 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;
        }
    });
}

I am not allowed to run this task at that line ?

Here is the class

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

Error Logcat:-

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)
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Constantin
  • 2,288
  • 2
  • 24
  • 31

1 Answers1

0

You are using a version of Smack that is incompatible with Android, apparently. IIRC, there is an aSmack library that you should be using.

You can tell that this is a problem with Smack because of the exception:

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

org.jivesoftware.smack is the Smack library.

You will also note that this is not android.os.NetworkOnMainThreadException, but java.lang.VerifyError.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491