I am a beginner in Android platform and i have encountering a serious problem. I want to fetch emails from gmail and therefore i have created a class that does that. However when i run my class in android NetworkOnMainThreadException. So i have created new Thread with a new Runnable as below :
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
FetchEmails e = new FetchEmails();
}
}).start();
}
It worked fine but then i can’t seem to have access to e.getMessage in order to get the actual email. Having this on mind i have implement Runnable to my FetchEMails class and done all the fetch code in run() which i have overidden. The last piece of code in run() is setting a variable to ‘true’. Then in my Activity class i have instantiate the class and i have done this : new Thread(e).start();. Below i have a while loop to check wether the class has finished to the end of run() method (by checking if the variable is true). So now i have created my new theard which does not intefere with the UI and when the code is finished i want to get the message. But it seems that i still get the NetworkOnMainThreadException error. Any help with that is apprecieted. Thanks.
You should be using an AsyncTask instead.