AsyncTask's doInBackground()
method already runs in a separate thread. From your Edit, you are starting another thread from inside of doInBackround()
. This is not necessary.
It seems that you want the code following the execution of your AsyncTask to wait for its completion. If this is the case, it would be better to use the onPostExecute()
method of the AsyncTask.
Your SignInActivit
should look like:
String login = e1.getText().toString();
String password = e2.getText().toString();
login = login.replace(" " , "");
password = password.replace(" " , "");
// Now, execute your AsyncTask, no need for `zalogujSie()`
new Logowanie(this, login, password).execute();
You don't need the intermediary step of calling zalogujSie()
. You can pass the login
& password
as arguments to the constructor. AsyncTask as:
public static class Logowanie extends AsyncTask<Void, Void, Void> {
Activity wywolujaceActivity;
private ProgressDialog dialog;
private String user, pass, error;
public Logowanie(Activity wywolujaceActivity, login, password) {
this.wywolujaceActivity = wywolujaceActivity;
dialog = new ProgressDialog(wywolujaceActivity);
this.user = login;
this.pass = password;
}
protected void onPreExecute() {
this.dialog.setTitle("Please wait...");
this.dialog.setMessage("Getting data from server");
this.dialog.show();
}
@Override
protected String doInBackground(Void... nothing) {
error = "";
final JSONParser jParser = new JSONParser();
final String json = jParser.getJSONFromUrl("http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + user + "&password=" + pass + "&imei=");
JSONObject jObject;
try {
jObject = new JSONObject(json);
Log.wtf("Link", "http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + user + "&password=" + pass + "&imei=");
Log.wtf("Link", json);
error = jObject.getString("error");
if(error.equals("You reached daily query limit !")) { nadajWartosc("You reached daily query limit !"); }
if(error.equals("0")) {nadajWartosc(jObject.getString("token"));}
if(error.equals("1")) {nadajWartosc("1");}
if(error.equals("Invalid username")) {nadajWartosc("Invalid username");}
if(error.equals("Invalid password")) {nadajWartosc("Invalid password");}
if(error.equals("This user is already logged in !")) {nadajWartosc("This user is already logged in !");}
} catch (JSONException e1) {
e1.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
// You had an "and"ing these conditions instead of "or"ing which would always return false.
if(error.equals("Invalid username") || error.equals("Invalid password") || error.equals("1"))
t1.setText("Inserted incorrect data");
if(error.equals("You reached daily query limit !"))
t1.setText("You reached daily query limit !");
if(error.equals("This user is already logged in !"))
t1.setText("This user is already logged in !");
}
}