I have a problem which happens when my users register with mysql.
When a username and password are registered, the app must show the message "User created successfully".
But the app doesn't show any message and instead closes itself.
The username has been inserted into the database.
It gives me the same problem even when I send empty data.
This my PHP response
$response["success"] = 1;
$response["message"] = "Username Successfully Added!";
echo json_encode($response);
This is my register Java code
Log.d("Login attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Username Successfully Added!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
I think it's because of
finish()
in your code. Generally,
finish()
destroy the Activity. That's why your app closed. Remove it and test it again. Hope this helps.
I believe your code could use a little bit of decoupling here using Events (Subscriber/Publish design pattern). I will give you some sort of idea or guideline so that you can give it a shot.
build.gradle
file, you want to register for your activity to listen for a given event - EventBus will notify whoever is listening for notifications. You register for instance inside onCreate()
method of your activity by calling EventBus.getDefault().register(this)
then remember to unregister inside onDestroy like this: EventBus.getDefault().unregister(this)
LoginCompletedEvent
this should have whatever variables you need to pass back to your UI or whoever is listening for updates from a background operation like making HTTP requests.onPostExecute()
method, you can notify your activity whether login operation or registration was successful by doing the following: EventBus.getDefault().post(new LoginCompletedEvent(true, message))
. This passes the response message to the subscriber so you can update the UI accordingly.Finally, inside your Activity or Fragment (Subscriber), you simply add a method with the following signature to get notified and catch the data passed back:
public void onEvent(LoginCompletedEvent event){ //do something with the feedback here}
That is all you need to accomplish your tasks using a clean Publisher/Subscriber design pattern which is provided to you by EventBus.
I hope this suggestion helps you. If you have any farther needs or clarification questions, let me know because I will be happy to help!