AsynkTask arguments confusion - android

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

Related

Sendind http json request from AsyncTask

Here is small example for to understand my question:
public class InitSettings_Task extends AsyncTask<Void, Void, Integer> {
#Override
protected Integer doInBackground(Void... params) {
request1result = request1;
if (request1result) {
result = httprequest2;
} else {
result = httprequest3;
}
return result;
}
#Override
protected void onPostExecute(Integer result) {
//do something with result
}
}
I know that Volley is a super library, but here i can't use it because my AsyncTask can ends before i will receive answer of first request.
Can somebody help me to understand what the best style for to send http request for this logic?
Before i have used Volley with Sleep() for to wait answer, but from my view it's not best sollution
Sounds like you are trying to do this -
public class InitSettings_Task1 extends AsyncTask<Void, Void, Integer> {
#Override
protected Integer doInBackground(Void... params) {
request1result = request1;
return result;
}
#Override
protected void onPostExecute(Integer result) {
//do something with result
if (request1result) {
result = new InitSettings_Task2().execute(httprequest2);
} else {
result = new InitSettings_Task2().execute(httprequest3);
}
}
}
public class InitSettings_Task2 extends AsyncTask<Void, Void, Integer> {
#Override
protected Integer doInBackground(Void... params) {
return result;
}
#Override
protected void onPostExecute(Integer result) {
//do what you want with result ?
}
}
But I would advise you against this. Its better to do this using frameworks like RxJava or even EventBus which are better suited for this scenario.
I have realized it with OkHTTP library. Thank's "Selvin" for right direction )

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.

Can't Override onPostExecute() method in AsyncTask Class

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.

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