Can't Override onPostExecute() method in AsyncTask Class - android

I know there are several question which are quite similar to this one, but they didn't helped me so far. I'm still not able to override this AsyncTasks.
Can you hel me?
I already made sure that doInBackground() devlivers some type that onPostExecute() requires (Boolean in the first and JSON in the second example).
Do you see anything except that i extend AsyncTask as raw type (was not sure about what to enter here)
This is the error eclipse tells me for each method:
The method onPostExecute(Boolean) of type Register.NetCheck must override or implement a supertype method
and for the classes eclipse tells me:
The type Register.NetCheck must implement the inherited abstract method AsyncTask.doInBackground(Object...)
AsyncTask is a raw type. References to generic type AsyncTask should be
parameterized
EXAMPLE 1:
private class NetCheck extends AsyncTask {
#Override
protected void onPreExecute(){
super.onPreExecute();
do.something();
}
#Override
protected Boolean doInBackground (String... args){
if (do.something.worked){
return true; }
else { return false}
}
#Override
protected void onPostExecute(Boolean th){
evalute.input.boolValue();
}
}
EXAMPLE 2:
private class ProcessRegister extends AsyncTask {
#Override
protected void onPreExecute() {
super.onPreExecute();
show.some.dialog();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONObject json = generate.some.json();
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
evaluate.something();
}
}

Your async task is not expecting a return value, so it does not recognise
protected void onPostExecute(Boolean th)
Try this instead:
private class NetCheck extends AsyncTask<String, Void, Boolean> {
#Override
protected void onPreExecute(){
super.onPreExecute();
do.something();
}
#Override
protected Boolean doInBackground (String... args){
if (do.something.worked){
return true; }
else { return false}
}
#Override
protected void onPostExecute(Boolean th){
evalute.input.boolValue();
}
}
Replace Boolean with JSONObject for the second example. Try to always use the IDE to automatically implement the methods for you, will avoid problems like this.

You should extend AsyncTask with types. See example below.
public class MyTask extends AsyncTask<Void, String, Integer> {
#Override
protected Integer doInBackground(Void... params) {
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
#Override
protected void onCancelled(Integer integer) {
super.onCancelled(integer);
}
#Override
protected void onCancelled() {
super.onCancelled();
}
}
Replaces Void, String, Integer with apropriate types. More info is here.
About Eclipse. Delete in and install Android Studio.

Related

AsyncTask : return from interface callback in doInBackground

I would need some help to figure out how to return a doInBackground() value in AsyncTask which code is an interface. Here is an example
private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
public MyAsyncTask() {
super();
// do stuff
}
#Override
protected Boolean doInBackground(Void... void) {
checkIfContentAvailable(new interfaceMediaAvailableToDownload() {
#Override
public void onComplete(boolean resutl) {
return result; //This must be doInBackground return value, not onSuccess which is Void
}
#Override
public void onInternetError() {
return false; //This must be doInBackground return value, not onSuccess which is Void
}
};
}
#Override
protected void onPostExecute(Boolean result) {
if (result){
//Do stuff
}else{
//Do stuff
}
}
}
Obviously, this above code can't work because I don't know how to return onSuccess() value to doInBackground().
I hope this is clear enough....
EDIT
Okay my bad, I thought it would have been more readable to hide MyInterface usage, but I realize through your answers it is not. So I completed the code to add more details.
Any idea please?
Thank you in advance.
Create the object of the Mynterface in the place where you execute the AsyncTask.
Create a object reference of the MyInterface inside the AsyncTask ans set your interface object.
Then call the onSuccess method like below
`
private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
MyInteface myInterface;
setMyInterface(MyInterface interface){this.myInterface = interface;}
public MyAsyncTask() {
super();
// do stuff
}
#Override
protected Boolean doInBackground(Void... void) {
this.myInterface.onSuccess(); // or call on failure if failure happened
}
#Override
protected void onPostExecute(Boolean result) {
//Do stuff with result
}
}
`
Use it like ...
MyAsyncTask async = new MyAsyncTask();
async.setMyInterface(this);
async.execute();
Implement the interface in the place where your are executing.
This how you can do it.

A `doInBackground()` of type void is not accepted by `AsyncTask`?

As seen from Google's Android Documentation we see that
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.
also mentioned in this excellent answer.
But then, why does having Void as result type result in the following warning by Android Studio on the doInBackground line:
doInBackground(String...)' in '(..)MainActivity.myTask' clashes with 'doInBackground(Params...)' in 'android.os.AsyncTask'; attempting to use incompatible return type
This is how the AsyncTask is declared:
private class myTask extends AsyncTask<String,Void,Void> {
#Override
protected void doInBackground(String... url) {
//do stuff with url[0]
}
#Override
protected void onPostExecute() {
//more stuff
}
}
In comparison, this seems to work fine:
private class myTask extends AsyncTask<String,Void,String> {
#Override
protected String doInBackground(String... url) {
//do stuff with url[0]
return "hi";
}
#Override
protected void onPostExecute(String result) {
//more stuff
}
}
Note there are various questions about this topic, but most of them just messed up the AsyncTask parameters or parameters and return values in the class.
try this code
private class myTask extends AsyncTask<String,Void,Void> {
#Override
protected Void doInBackground(String... url) {
//do stuff with url[0]
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//more stuff
}
}

asyncTask not working for me

I have an IME service class and a long operation method in it. I want to run the LongOperation task in a asyncTask class that is in the IME Service class.
public class Myimeservice extends InputMethodService
implements KeyboardView.OnKeyboardActionListene {
//...
//some code here....
//...
public void setDictionary(){
//....
}
private class LongOperation extends AsyncTask<String, Void, String> {
private Myimeservice parent;
public LongOperation(Myimeservice pim){
parent = pim;
}
#Override
protected String doInBackground(String... params) {
Myimeservice tmp = new Myimeservice();
tmp.setDictionary();
return "Executed";
}
#Override
protected void onPostExecute(String result) {
//app.hideLoading();
}
#Override
protected void onPreExecute() {
//app.showLoading();
}
#Override
protected void onProgressUpdate(Void... values) {}
}
When i run it, the application forced to close. please help me.
I think the error is somewhere in your public void setDictionary() method.
I assume that you are manipulating a variable that is bound to the UIThread/MainThread, the application will crash since doInBackground is on another Thread.
Instead make the setDictionary() method return the dictionary and return it instead of "Executed" in doInBackground().
This will call the onPostExecute(Object result) which is run on UIThread/MainThread.
Something like this:
private class LongOperation extends AsyncTask<String, Void, Dictionary> {
#Override
protected Dictionary doInBackground(String... params) {
Myimeservice tmp = new Myimeservice();
Dictionary dict = tmp.setDictionary();
return dict;
}
#Override
protected void onPostExecute(Dictionary result) {
//do what ever you meant to do with it;
}
}
If you are not expecting any result from it you can just do:
AsyncTask.execute(new Runnable() {
#Override
public void run() {
tmp.setDictionary();
}
});
I use the Runnable instead of AsyncTask and the problem solved.
final Runnable r = new Runnable(){
public void run(){
setDictionary();
}
};
this code is in onCreate() method of service.
Tanks a lot Tristan Richard.

AsynkTask arguments confusion

I have the following fully working code which was cobbled together with a bit of cut & paste from other people's examples:
private class Get_tweets_async_task_class extends AsyncTask<String, Integer, Bitmap>
{
protected void onPreExecute()
{
}
#Override
protected Bitmap doInBackground(String... params)
{
array_of_single_tweets = getTweets(params[0], 1);
return null;
}
protected void onProgressUpdate(Integer... params)
{
}
protected void onPostExecute(Bitmap img)
{
update_display_list();
}
protected void onCancelled()
{
}
}
My code does not in fact need anything to do with bitmaps (that was a hangover from the example) and now I'm trying to get rid of it. I edited the code as follows:
private class Get_tweets_async_task_class extends AsyncTask<String, Integer, Void>
{
protected void onPreExecute()
{
}
#Override
protected Void doInBackground(String... params)
{
array_of_single_tweets = getTweets(params[0], 1);
return null;
}
protected void onProgressUpdate(Integer... params)
{
}
protected void onPostExecute()
{
update_display_list();
}
protected void onCancelled()
{
}
}
but now the code no longer works. Did I do something wrong in my removal of the bitmap?
Edit: Sorry I mistyped the onPostExecute line - now corrected
protected void onPostExecute(Void nothing)
You need to fix your onPostExecute method.
It still receiving Bitmap as a result, change it to Void.
You edit your post but the problem is the same in onPostExecute.
Please check the official documentation : http://developer.android.com/reference/android/os/AsyncTask.html

AsyncTask onPostExecute not being called

The project I'm working on is slightly more complicated but I made this simple test to try to track down what was wrong with my code. The progress dialog never dismisses. I had it at one point where they weren't returning null. '
public class SyncTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new mTask(this).execute();
}
public class mTask extends AsyncTask<Void, Void, Void> {
Context mContext;
ProgressDialog progressDialog;
public mTask(Context aContext) {
mContext = aContext;
}
#Override
public void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("New...");
progressDialog.show();
}
#Override
public Void doInBackground(Void... params) {
return null;
}
public Void onPostExecute(Void... params) {
progressDialog.dismiss();
return null;
}
}
}
The parameters are wrong, use this:
#Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
return;
}
I am agree with Cesar and Shailendra answers, but still let me make little improvement over it:
#Override
protected void onPostExecute(Void result) {
if(progressDialog.isShowing())
{
progressDialog.dismiss();
}
return;
}
Missing #Override notation before onPostExecute. Also return null is not required.

Categories

Resources