需要帮助调试电子邮件code [英] Need Help Debugging Email Code
问题描述
我想知道,如果有人可以帮助我调试。我目前使用code的:
I was wondering if someone could help me with debugging. I am currently using the code in:
<一个href=\"http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a\">Sending使用JavaMail API,而无需使用电子邮件中的Android默认/内置应用
和已使用所有code的指示。输入我的电子邮件字段后,我无法获得电子邮件发送。我注意到GMailSender.java,它似乎有code的缺失一块一块code的。
and have used all of the code as directed. After inputting my email fields, I was unable to get email to be sent. I noticed a piece of code in GMailSender.java, where it seemed that there was a missing piece of code.
}catch(Exception e){
}
知道我已经调试了code的每一个方面,我加了一个Log.e,像这样。
Knowing that I had debugged every other part of the code, I added a Log.e, as so.
}catch(Exception e){
Log.e("GmailDebug", e.getMessage(), e);
}
其结果是,我注意到,我得到了以下日志。
As a result, I noticed that I got the following logs.
01-16 22:22:38.933: E/GmailDebug(4487): null
01-16 22:22:38.933: E/GmailDebug(4487): android.os.NetworkOnMainThreadException
先注释掉线,系统地取消注释在他们之后,我终于能够确定,我没有得到任何日志,直到我注释掉行
After commenting out the lines beforehand and systematically uncommenting them, I finally was able to determine that I didn't get any logs until I uncommented the line
Transport.send(message);
有人能帮助我,并告诉我,我该怎么prevent这些错误?谢谢!
Can someone help me out, and tell me how I can prevent these errors? Thanks!
推荐答案
这是因为你在主线程中进行网络操作,这是不允许在Android 3.0及以上。即使是在服务,服务是在UI线程上运行,除非您特别推出他们在另一个线程或创建里面的线程。
This happens because you are doing a network operation on the main thread, and this is not allowed on Android 3.0 and above. Even though it is in a service, services are run on the UI thread unless you specifically launch them in another thread or create a thread inside it.
您可以通过运行在后台任务线程关闭主UI线程,使用的发或 AsyncTask的。
You can fix this by running the task in a background thread off the main UI thread, by using a Thread or an AsyncTask.
这篇关于需要帮助调试电子邮件code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!