android.os.NetworkOnMainThreadException从安卓发送电子邮件 [英] android.os.NetworkOnMainThreadException sending an email from Android
本文介绍了android.os.NetworkOnMainThreadException从安卓发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经写了从Android设备发送邮件的应用程序,但是当我尝试发送一封电子邮件,我得到以下异常:
I have written an application which sends email from an Android device but I get the following exception when I try to send an email:
android.os.NetworkOnMainThreadException
为什么会发生这种情况,我该如何解决这个问题?
Why is this occurring and how can I fix it?
推荐答案
哪个SDK版本?如果14+看到此链接。
Which SDK version? If 14+ see this link.
的解决方案是
JUST FOR DEBUG
添加这些行
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
真实案例 把code上的AsyncTask
Real Case Put the code on an AsyncTask
private class Connection extends AsyncTask {
@Override
protected Object doInBackground(Object... arg0) {
connect();
return null;
}
}
然后调用
new Connection.execute("");
这篇关于android.os.NetworkOnMainThreadException从安卓发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文