onPostExecute does not get triggered - java

So I have created a webservice call class that calls my webservice which extends on AsyncTask:
public class WebServiceLoginCall extends AsyncTask{
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
protected void onPostExecute(Void result) {
try {
if(loginStatus){
System.out.println("onpost has been executed");
//Navigate to Home Screen
loginIntent = new Intent(getBaseContext(), HomeActivity.class);
startActivity(loginIntent);
//prevents user to go back to the login screen
finish();
}else{
//Set Error message
Toast.makeText(getApplicationContext(), "Login Failed. Check your details.", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "An error occured. Check your mobile connection.", Toast.LENGTH_SHORT).show();
}
}
#Override
protected Boolean doInBackground(Object... arg0) {
System.out.println("doinbackground triggered");
try {
loginStatus = Webservice.invokeLoginWS(loginUser.get_userEmail(), loginUser.get_userPassword());
} catch (Exception e) {
System.out.println("an error occured with the webservice");
}
return loginStatus;
}
}
When the user presses the login button, I use the following code:
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:
email = (EditText) findViewById(R.id.txtEmail);
loginUser = new User();
loginUser.set_userEmail(email.getText().toString());
loginUser.set_userPassword(password.getText().toString());
//starts loging webservice
WebServiceLoginCall task = new WebServiceLoginCall();
//executes the login task
task.execute();
break;
When I check, the doInBackground is triggered in my console, but the onPostExecute is not. Is there anything that I am doing wrong? The doInBackground does not throw any exceptions.

Make following two changes
1.Use public class WebServiceLoginCall extends AsyncTask<Void, Void, Boolean >
instead of public class WebServiceLoginCall extends AsyncTask
2.Use
#Override
protected void onPostExecute(Void result) {
// your code
}
instead of just
protected void onPostExecute(Void result) {
// your code
}
Refer Android- Async task
Explanation:
In your case if you put #Override on onPostExecute() without extending the generic Asynctask, you will get a compile time error. Hence you will have to make above two changes.
hope it helps you.

because you are calling a different method your post execute wont execute ever execute if you defined it like your code
use
#Override
protected void onPostExecute(boolean result) {
}

Because your not Override the onPostExecute() method
add : #Override
like this
#Override
protected void onPostExecute(Void result) {
try {
if(loginStatus){
System.out.println("onpost has been executed");
//Navigate to Home Screen
loginIntent = new Intent(getBaseContext(), HomeActivity.class);
startActivity(loginIntent);
//prevents user to go back to the login screen
finish();
}else{
//Set Error message
Toast.makeText(getApplicationContext(), "Login Failed. Check your details.", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "An error occured. Check your mobile connection.", Toast.LENGTH_SHORT).show();
}
}

1) Add the #Override to your postexecute
2) If your asynctask takes parameter then execute your asynctask like this:
task.execute(Object);
because your AsyncTask takes Object
#Override
protected Boolean doInBackground(Object... arg0)
3) Your doInBackground returns a Boolean loginStatus value but your onPostExecute(Void result) takes in void.
Hope it helps

Related

Only the original thread that created a view hierarchy can touch its views error in asynctask

I'm using View.OnClickListener. Code is as given below:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.appCompatButtonLogin:
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();
break;
case R.id.textViewLinkRegister:
// Navigate to RegisterActivity
Intent intentRegister = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(intentRegister);
break;
}
}
My AsyncTask class is like this:
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
#Override
protected void onPreExecute() {
if (progressDialog == null) {
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.setMessage("Please wait!");
progressDialog.show();
}
super.onPreExecute();
}
#Override
protected String doInBackground(String... strings) {
try {
verifyFromSQLite();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void verifyFromSQLite() {
if (inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
return;
}
if (inputValidation.isInputEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
return;
}
if (inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, getString(R.string.error_message_email))) {
return;
}
if (databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim()
, textInputEditTextPassword.getText().toString().trim())) {
Intent accountsIntent = new Intent(activity, UsersListActivity.class);
accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
emptyInputEditText();
startActivity(accountsIntent);
} else {
Toast.makeText(LoginActivity.this, "Please check your credentials", Toast.LENGTH_SHORT).show();
}
}
private void emptyInputEditText() {
textInputEditTextEmail.setText(null);
textInputEditTextPassword.setText(null);
}
#Override
protected void onPostExecute(String s) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
When I run my code, I get an exception like this:
Only the original thread that created a view hierarchy can touch its views
I'm trying to separate UI from non-UI part in asynctask, how can I fix this?
You can't make changes to UI in a background task.
Move this code:
Intent accountsIntent = new Intent(activity, UsersListActivity.class);
accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
emptyInputEditText();
startActivity(accountsIntent);
and
Toast.makeText(LoginActivity.this, "Please check your credentials", Toast.LENGTH_SHORT).show();
to onPostExecute().
You can set values to boolean flags for these cases in doInBackground() and check them in onPostExecute() and act accordingly.

onProgressUpdate is not running

I'm receiving json data from an api using Retrofit, and works perfectly, but in the future I want to show a progress bar. I decided to implement the onProgressUpdate in order to check if this method is invoked, but when checking the Logcat tab, it's not working. That is, the method is not running. All other methods inside the AsyncTask class are working. I have a Toast for each method and all of these run.
public class MindicadorTask extends AsyncTask<Void, Void, Void> {
private String TAG = MindicadorTask.class.getSimpleName();
#Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this, "JSON Downloading", Toast.LENGTH_SHORT).show();
}
#Override
protected Void doInBackground(Void... voids) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(IMindicador.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
IMindicador iMindicador = retrofit.create(IMindicador.class);
Call<Mindicador> call = iMindicador.getAPI();
mMindicador = new Mindicador();
try {
mMindicador = call.execute().body();
} catch (IOException e) {
Log.e(TAG, "" + e.getMessage());
}
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
Log.e(TAG, "onProgressUpdate was called");
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(MainActivity.this, "JSON Call was sucessfull", Toast.LENGTH_SHORT).show();
}
}
You have to pass it some value, instead of Void. Try using an int, for example. As the int iterates, it calls onProgressUpdate().
Specify the type data which you want to send to onProgressUpdate method.also call publishProgress method inside doInBackground method which trigers onProgressUpdate method call.

How to show ProgressDialog when AsyncTask start gathering data from the server till when it gets all the data

I would like to make an app which displays some data from the server. When I log in as an admin, I would like there to be a progress dialog until the application gets all the data from the server.
I have 3 Classes. Main Activity(login screen), SecondActivity(displays data) and BackgroundWorker(which extends AsyncTask).
I know that in on postExecute I have to close ProgressBar
Override
protected void onPreExecute() {
if(activity.getClass() == MainActivity.class) {
this.progressDialog.setMessage("Please wait for a while.");
this.progressDialog.setTitle("Login");
this.progressDialog.show();
}
else
super.onPreExecute();
}
#Override
protected void onPostExecute(final String result) {
if(activity.getClass() == MainActivity.class) {
new CountDownTimer(1000, 500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
System.out.println(result);
if (result.equals("Username or password is not correct")) {
alertDialog.setMessage(result);
alertDialog.show();
} else if(result.equals("is Admin")) {
Intent intent = new Intent(activity,Admin.class);
intent.putExtra("username",user);
activity.startActivity(intent);
activity.finish();
}
progressDialog.dismiss();
}
}.start();
}
I have made like this for login Screen but I don't think it is wise to delay the application on purpose. And also my implementation doesn't work if I call AsyncTask class twice in one activity. Any suggestion?
You can use this code:
class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("loading");
pd.show();
}
#Override
protected Void doInBackground(Void... params) {
// Do your request
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pd != null)
{
pd.dismiss();
}
}
}
Take a look at this link, if you want!
Good luck with your android development!

Android move from one activity to another

I was having some problem for Android activity transition. Basically what I am trying to do is share text to Twitter. However, when I open up the twitter content, it took quite a few seconds to load up the content and resulting in the white blank activity for a few seconds.
And here is my codes, when my button onClick, I am executing the loading dialog:
ivTwitterShare.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Thread newThread = new Thread() {
#Override
public void run() {
try {
super.run();
sleep(10000);
} catch (Exception e) {
} finally {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(tweetUrl));
startActivity(intent);
progressDialog.dismiss();
}
}
};
newThread.start();
new LoadTwitterTask().execute();
}
});
private class LoadTwitterTask extends AsyncTask<Void, Integer, Void> {
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "Loading Twitter...",
"Retrieving Twitter information, please wait...", false,
false);
EventDialogueBox.customizeDialogueBox(context, progressDialog);
}
#Override
protected Void doInBackground(Void... params) {
try {
synchronized (this) {
int counter = 0;
while (counter <= 4) {
this.wait(50);
counter++;
publishProgress(counter * 25);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]);
}
#Override
protected void onPostExecute(Void result) {
}
}
However, my problem now is the white blank page before the content is loaded up still there. What I wanted is firstly, the loading dialog will show. Then, at the same time, the twitter intent is loading. Once finish loaded up the content, then dialog will be dismissed.
Any ideas?
Thanks in advance.

Errors canceling AsyncTask "Cannot make a static reference to the non-static method cancel(boolean) from the type AsyncTask<Void,Integer,Void>"

I am trying to make it so that when the user pushes a button than it cancels the Asynk task and then finishes the activity but i keep getting errors with the "MyAsyncTask.cancel(true);" if there is additional info you need please tell me.
here is what I got,
class MyAsyncTask extends AsyncTask<Void, Integer, Void>{
boolean DoingSomething;
int progress_status;
#Override
protected void onPreExecute() {
super.onPreExecute();
Log.v("UpGradeRep", "started");
DoingSomething = true;
UpGradeRep.setVisibility(View.GONE);
UpGradeRepDis.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,
"started", Toast.LENGTH_SHORT).show();
// progress_status = 0;
}
#Override
protected Void doInBackground(Void... params) {
while (!isCancelled()) {
while(DoUpCount==true){
progress_status += 1;
publishProgress(progress_status);
SystemClock.sleep(200);
if(isCancelled()){
break;
}
}
}
return null;
}
int UpGradRepcounter = 0;
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
UpGradRepcounter++;
DevUpCountDis.setText(" "+UpGradRepcounter);
Money = Money +inc;
MoneyDis.setText("$"+Money);
}
#Override
protected void onCancelled() {
Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_SHORT);
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
DoUpCount=false;
DoingSomething = false;
Log.v("UpGradeRep", "Done");
}
}
here's what i do when i am trying to close it
Statistics.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MyAsyncTask.cancel(true);
//^^^^Right here is where it tells me "Cannot make a static reference to the non-static method cancel(boolean) from the type AsyncTask<Void,Integer,Void>"
Intent GoToStatistics = new Intent(getApplicationContext(),statistics.class);
GoToStatistics.putExtra("LittleHelper", LittleHelper);
startActivity(GoToStatistics);
finish();
}
});
so thats the code i set up to do what i whant and it dose good but when i leave it gives a frame drop warning in my Logcat so i was ausuming that im not closeing it properly
im starting it like this
so when the user pushes the button than it starts it
UpGradeRep.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("unchecked")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Money>=upGM2) {
LittleHelper=true;
DoUpCount=true;
Log.v("DoUpCount"," "+DoUpCount);
(new MyAsyncTask()).execute();
}else{
Error.setText("you need more money.");
}
}
});
You're trying to call a metod over a non-static object within a static context (the onClick()) event. Basically what that error is telling you is that you must make your AsyncTask object static in order to be able to call the cancel() method over it.
This is as "easy" as declaring your AsyncTask as static, but that could generate some other compilation errors over your AsyncTask object, and you'll have to be very thorough to avoid possible memory leaks.
The method cancel(boolean mayInterruptIfRunning) (in class AsyncTask) is not a static method and you're calling it with the class name (the way static methods are invoked).
You should use the instance of MyAsyncTask to call (this):
this.cancel(true);

Categories