Network-related Tasks On A Separate Thread
Solution 1:
Yes you are trying to do networking inside the UI thread which is illegal and catch an exception which is NetworkOnMainThreadException
.
Instead of connecting inside the main thread you can use the AsyncTask
which you are doing right now but it shouldn't be abstract it is just a plain class so you can execute the AsyncTask
..
example:
publicclassRetrievePollenTaskextendsAsyncTask<Integer, Void, Pollen>
{
protectedPollendoInBackground(String... params)
{
Pollen pollenObject = newPollen(19104);
return pollenObject;
}
protectedvoidonPostExecute(Pollen result) {
textview.setText(pollenObject.getCity());
}
}
you can make your asynctask as an inner class of you fragment so you dont need to pass the context parameter on it. Also dont put update any view insidte the doInBackground
cause it is a different thread that it will catch an exception.
Solution 2:
AsyncTask
is a mechanism provided by the Android framework to facilitate execution of long running tasks without blocking the main UI thread. Read Keeping Your Apps Responsive tutorial on the Android Developer sites for a detailed treatment on this subject.
This link provided should help you re-design your classes using the AsyncTask
mechanism.
Hope this helps
Solution 3:
This is a tutorial of AsyncTask
. You have to do all the network operations using AsyncTask
.
It basically has 3 methods
onPreExecute()
doInBackground()
onPostExecute()
do the network operations in doInBackground()
method. update the UI in onPostExecute()
classRetrievePollenTaskextendsAsyncTask<Integer, Void, Pollen>
{
@Overrideprotected Pollen doInBackground(Integer zip)
{
// TODO Auto-generated method stubPollenpollenObject=newPollen(zip);
return pollenObject;
}
@OverrideprotectedvoidonPostExecute(Pollen pollenObject) {
textView.setText(pollenObject.getCity());
}
}
And inPlacholderFragment
class
button.setOnClickListener(newView.OnClickListener()
{
@OverridepublicvoidonClick(View v)
{
RetrievePollenTaskobject = newRetrievePollenTask();
object.execute(19104);
}
});
Solution 4:
Rule of thumb:
- Network (or other I/O or long-running) code goes in
doInBackground()
, which is executed in a background thread. - UI code goes in
onPostExecute()
, which is executed o the UI thread as soon as the background operation is finished.
In your example, this is probably the simplest code you could write:
button.setOnClickListener(newView.OnClickListener()
{
@OverridepublicvoidonClick(View v)
{
AsyncTask<Integer, Void, Pollen> loadPollen = newAsyncTask<Integer, Void, Pollen>()
{
@OverrideprotectedPollendoInBackground(Integer... params)
{
returnnewPollen(params[0]);
}
@OverrideprotectedvoidonPostExecute(Pollen result)
{
textview.setText(result.getCity());
}
};
loadPollen.execute(19104);
}
});
Solution 5:
An AsyncTask
is used to run code in a non UI thread but also has methods that can interact with your UI.
AsyncTask<String, Integer, Pollen>
This defines the Objects that the AsyncTask uses. Use Void
if no object is to be used.
- The first is an input for your
doInBackground()
. - The second is an argument for
onProgressUpdate()
to be used to periodically update the user on progress - The last is the result returned from
doInBackground()
These Arguments are declared with Object... vars
and are actually arrays that you would access with vars[index]
. If you only ever pass one variable access it with var[0]
Here is the official documentation
classRetrievePollenTaskextendsAsyncTask<String, Integer, Pollen>
{
@OverrideprotectedvoidonPreExecute() {
// set up a ProgressBar or other UI action here *before* the task runs
}
@OverrideprotectedPollendoInBackground(String... params)
{
// perform your separate non UI thread actions here// call publishProgress(int) to update your UI periodicallyreturn pollenObject;
}
@OverrideprotectedvoidonProgessUpdate(Integer... progress) {
// update your UI or ProgressBar
}
@OverrideprotectedvoidonPostExectute(Pollen resultReturned)
// back to the UI thread again here to update Views or cancel your ProgressBar etc// this executes *after* your task has completed// it receives its argument from the return of doInBackground() so you can use that when updating your UI.
}
}
Post a Comment for "Network-related Tasks On A Separate Thread"