I try to log in when clicking the button. I can see from the log that I have entered the correct entries. But it stays in this activity.
Here is the LoginActivity.java(When i press login, i get toast message: STAP 3):
public class LoginActivity extends Activity {
Button btnLogin;
Button btnLinkToRegister;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.i("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json != null && json.getString(KEY_SUCCESS) != null) {
Toast.makeText(getApplicationContext(),
"STAP 1", Toast.LENGTH_LONG).show();
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent myactivity = new Intent(getApplicationContext(), MyActivity.class);
// Close all views before launching Dashboard
myactivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myactivity);
Toast.makeText(getApplicationContext(),
"STAP 2", Toast.LENGTH_LONG).show();
// Close Login Screen
finish();
}else{
Toast.makeText(getApplicationContext(),
"Error logging innnn!", Toast.LENGTH_LONG).show();
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(),
"STAP 3", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
}
}
Here is the UserFunctions.java:
public class UserFunctions {
public JSONParser jsonParser;
private static String loginURL = "-----";
private static String registerURL = "------";
private static String login_tag = "login";
private static String register_tag = "register";
// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}
/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
JSONObject json;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
// Log.e("JSON", json.toString());
return json;
}
/**
* function make Login Request
* @param name
* @param email
* @param password
* */
public JSONObject registerUser(String name, String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}
/**
* Function get Login status
* */
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}
/**
* Function to logout user
* Reset Database
* */
public JSONObject logoutUser(Context context){
JSONObject json;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", null));
params.add(new BasicNameValuePair("password", null));
json = jsonParser.getJSONFromUrl(loginURL, params);
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return json;
}
}
logcat:
12-04 21:32:22.730 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ org.json.JSONException: No value for success
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:355)
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at org.json.JSONObject.getString(JSONObject.java:515)
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at com.example.thelegendaryturk.theneckoptimizer.LoginActivity$1.onClick(LoginActivity.java:67)
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at android.view.View.performClick(View.java:4438)
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 21:32:22.790 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
12-04 21:32:22.820 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
12-04 21:32:22.820 898-1224/com.example.thelegendaryturk.theneckoptimizer E/JSON﹕ {"error_msg":"Connected "}{"tag":"login","success":1,"error":0,"uid":"548108d1662e68.06979757","user":{"name":"pok","email":"pok","created_at":"2014-12-05 02:22:25","updated_at":null}}
12-04 21:32:22.820 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
12-04 21:32:22.820 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
12-04 21:32:22.840 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-04 21:32:22.840 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-04 21:32:22.860 898-898/com.example.thelegendaryturk.theneckoptimizer W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
I ran this code locally on a phpmyadmin, it worked. I swapped to the database in the cloud and it does not work anymore... Any help is welcome as I am wasting my time for hours now....
To make it a bit more clear: It does not access the if statement within try{}