Solved Thank you guys i resolved my problem by removing PreExecute funtion from my AsyncTask function. Thanks all.
i want to return Integer value which is userId to other activities from a AsycnTask function, I tried googling my problem and saw a lot of discussions on stackoverflow but none solved my problem...Hope you guys can guide me how to do this.
Final Code: (After solving problem)
private class ValidateUserAccount extends AsyncTask<Void, Integer, Void> {
String response = "";
@Override
protected Void doInBackground(Void... voids) {
HashMap<String, String> loginDataParams = new HashMap<>();
loginDataParams.put("email", strEmail);
loginDataParams.put("password", strPassword);
JSONObject jsonData = new JSONObject(loginDataParams);
String path = "myUrl";
response = Login.ServerData(path, jsonData);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
uProgressBar.setVisibility(View.GONE);
int code = Login.passResCode();
userId = Login.passUserId();
if (code == 200) {
Toast.makeText(getApplicationContext(), "Successfully Login: " + userId, Toast.LENGTH_SHORT).show();
userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
Intent loginSuccess = new Intent(getApplicationContext(), MainActivity.class);
userIdEditor = userIdSharedPreferences.edit();
userIdEditor.putInt("userId", userId);
userIdEditor.apply();
startActivity(loginSuccess);
finish();
} else {
final AlertDialog.Builder alertBox = new AlertDialog.Builder(LoginActivity.this);
String errorMessage = "";
if (code == 401)
errorMessage = "Email and Password are not valid.";
else if (code == 0)
errorMessage = "Please check your internet connection.";
alertBox.setMessage(errorMessage).setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = alertBox.create();
alert.setTitle("Oops, Something went wrong!");
alert.show();
}
}
}