StuckBetweenTrees StuckBetweenTrees - 7 months ago 41
Java Question

Displaying null values after first users login inside android

I have 3 fragments inside an app and in one of them I display users name from the SQLite database. What happens is when I register a new user and login first time with it, inside the textview where the users name suppose to appear, it displays NULL value, but when I logout and login again with the same user, name appears as it should.

After registering user, all the data is inserted inside a database, I have checked.

Any ideas what can cause this problem? I will add some code on request as I have no idea which part of the code, fragments or java files might cause this..

EDITED

I have added some code to help resolve this issue.

Login function inside the main screen (once app launches):

private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";

pDialog.setMessage("Logging in ...");
showDialog();

StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {

@Override
public void onResponse(String response) {

Log.d(TAG, "Login Response: " + response.toString());
hideDialog();


try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");

// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Now store the user in SQLite
String uid = jObj.getString("uid");

JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user.getString("created_at");

// Inserting row in users table
db.addUser(name, email, uid, created_at);

// Launch main activity
Intent intent = new Intent(Main.this,
Logged.class);
startActivity(intent);
finish();
} else {

error_msg.setVisibility(View.VISIBLE);
String msg = jObj.getString("error_msg");
error_msg.setText(msg);
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {

@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);

return params;
}

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}


onViewCreated func inside fragment where users name should be diplayed:

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

profileName = (TextView) getActivity().findViewById(R.id.profileName);


// SqLite database handler
db = new SQLiteHandler(getActivity().getApplicationContext());

// session manager
session = new SessionManager(getActivity().getApplicationContext());

if (!session.isLoggedIn()) {
logoutUser();
}

// Fetching user details from SQLite
HashMap<String, String> user = db.getUserDetails();

String name = user.get("name");

// Displaying the user details on the screen
profileName.setText(name);

}


Register function part:

public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();

try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {

// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");

JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user.getString("created_at");

// Inserting row in users table
db.addUser(name, email, uid, created_at);

// Launch login activity
Intent intent = new Intent(
Register.this,
Main.class);
startActivity(intent);
finish();
} else {

// Error occurred in registration. Get the error
// message
error_msg.setVisibility(View.VISIBLE);
String msg = jObj.getString("error_msg");
error_msg.setText(msg);


}
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {

Answer

I managed to sort this problem myself.. The problem was not in java files but in php files sending to java for some reason..

In php function I used:

$stmt->bind_result($id, $unique_id, $name, $email, $encrypted_password, $salt, $created_at, $updated_at);
$user = $stmt->fetch();

But then changed to

$user = $stmt->get_result()->fetch_assoc(); 

And that fixed the problem..