I am creating an app, when you log in with user name and password, it requests the info from the server.
The server’s request takes time (15-20 sec), meanwhile I want to show a spinning bar with few words.
But I tried so many variations of the AsyncTask class, and I can’t get it to work. It gets the information okay, but freezes the screen until the response.
Right now I just have a new thread implementing runnable. I’m not sure from where in the code I need to call the AsyncTask .
The onClick triggers the attemptLogin() function:
public void onClick(View view) {
attemptLogin();
}
In attemptLogin() function:
// more code
showProgress(true);
new Thread(new GetServerResponseRunnable()).start();
while (wait) {}
// more code
And the Runnable is:
public class GetServerResponseRunnable implements Runnable {
@Override
public void run() {
response = getInfo.getTours(mUsername, mPassword);
wait = false;
}
}
Which as you can see call another function from a different class.
This is the function:
public String getTours(String username, String password) {
String req = "GETALLDATA";
String retStr = "";
try {
url = getURL(req, username, password);
sendOutputLine(url, "");
retStr = getReturnString();
Log.d(LoginActivity.DEBUG_TAG, "getTours() return: " + retStr);
} catch (Exception e) {
Log.d(LoginActivity.DEBUG_TAG, "programm bommed client: " + e.getMessage());
}
return retStr;
}
I need help, please. Mainly what I want to do is:
response = getInfo.getTours(mUsername, mPassword);
wait = false;
And show the spinning bar meanwhile.
Thanks
Update: 02.13.2013
I used this code, but I got a
02-13 09:07:16.142: E/AndroidRuntime(1046): java.lang.NullPointerException
in the line:
this.dialog.setMessage(getResources().getString(R.string.login_progress_signing_in));
Any idea why?
public class LoginTask extends AsyncTask<Object, Void, String> {
public Context context;
public ProgressDialog dialog;
public void BaseTask(Context context) {
this.context = context;
this.dialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
this.dialog.setMessage(getResources().getString(R.string.login_progress_signing_in));
this.dialog.show();
}
@Override
protected String doInBackground(Object... objects) {
String name = (String) objects[0];
String password = (String) objects[1];
String response = getInfo.getTours(name , password );
return response;
}
@Override
protected void onPostExecute(String response) {
if (dialog != null && dialog.isShowing())
dialog.dismiss();
LoginActivity.response = response;
// process response as you need
}
}
I think you need thomething like this
Call this taks