我想在用户点击DONE后发送简单的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');
}
}
错误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)
答案 0 :(得分: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
。