I try to process the json response sent by php and I get through the Volley response in this Android code, but after some debugging, I realized that the part of the code processing the json object does not execute strating from the JSONObject string jObj = new JSONObject (answer) ; I printed the magazine before it and printed it, so if you could help me identify the problem, I would be smart. Android code:
private void checkLogin(final String email, final String password) {
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
Log.d("Login","You're in the try portion of the code");
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
Log.d("Login", "user successfully logged in");
session.setLogin(true);
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, 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() {
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "login");
params.put("email", email);
params.put("password", password);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
php code:
<?php
header('Content-Type: application/json');
if (isset($_POST['tag']) && $_POST['tag'] != '') {
$tag = $_POST['tag'];
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array("tag" => $tag, "error" => FALSE);
if ($tag == 'login') {
$email = $_POST['email'];
$password = $_POST['password'];
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
} else if ($tag == 'register') {
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
if ($db->isUserExisted($email)) {
$response["error"] = TRUE;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
$user = $db->storeUser($name, $email, $password);
if ($user) {
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
$response["error"] = true;
$response["error_msg"] = "Error occured in Registartion";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameter 'tag' is missing!";
echo json_encode($response);
}
?>
answer in logcat:
09-16 02:14:11.785: D/LoginActivity(4314): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0029</td><td bgcolor='#eeeeec' align='right'>143512</td><td bgcolor='#eeeeec'>{main}( )</td><td title='D:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
09-16 02:14:11.785: D/LoginActivity(4314): <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0045</td><td bgcolor='#eeeeec' align='right'>158264</td><td bgcolor='#eeeeec'>DB_Functions->__construct( )</td><td title='D:\wamp\www\android_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>20</td></tr>
09-16 02:14:11.785: D/LoginActivity(4314): <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0060</td><td bgcolor='#eeeeec' align='right'>162616</td><td bgcolor='#eeeeec'>DB_Connect->connect( )</td><td title='D:\wamp\www\android_login_api\include\DB_Functions.php' bgcolor='#eeeeec'>..\DB_Functions.php<b>:</b>13</td></tr>
09-16 02:14:11.785: D/LoginActivity(4314): <tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0072</td><td bgcolor='#eeeeec' align='right'>163184</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a>
09-16 02:14:11.785: D/LoginActivity(4314): ( )</td><td title='D:\wamp\www\android_login_api\include\DB_Connect.php' bgcolor='#eeeeec'>..\DB_Connect.php<b>:</b>18</td></tr>
09-16 02:14:11.785: D/LoginActivity(4314): </table></font>
09-16 02:14:11.785: D/LoginActivity(4314): {"tag":"login","error":false,"uid":"55ef78acac6190.14514751","user":{"name":"Taha Souri 5","email":"taha_sr2002@yahoo.com","created_at":"2015-09-09 02:09:16","updated_at":null}}
09-17 01:03:17.081: W/System.err(24259): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
09-17 01:03:17.081: W/System.err(24259): at org.json.JSON.typeMismatch(JSON.java:111)
09-17 01:03:17.081: W/System.err(24259): at org.json.JSONObject.<init>(JSONObject.java:159)
09-17 01:03:17.081: W/System.err(24259): at org.json.JSONObject.<init>(JSONObject.java:172)
09-17 01:03:17.081: W/System.err(24259): at info.androidhive.loginandregistration.LoginActivity$3.onResponse(LoginActivity.java:104)
09-17 01:03:17.081: W/System.err(24259): at info.androidhive.loginandregistration.LoginActivity$3.onResponse(LoginActivity.java:97)
09-17 01:03:17.081: W/System.err(24259): at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-17 01:03:17.081: W/System.err(24259): at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-17 01:03:17.081: W/System.err(24259): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-17 01:03:17.082: W/System.err(24259): at android.os.Handler.handleCallback(Handler.java:808)
09-17 01:03:17.082: W/System.err(24259): at android.os.Handler.dispatchMessage(Handler.java:103)
09-17 01:03:17.082: W/System.err(24259): at android.os.Looper.loop(Looper.java:193)
09-17 01:03:17.082: W/System.err(24259): at android.app.ActivityThread.main(ActivityThread.java:5322)
09-17 01:03:17.082: W/System.err(24259): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 01:03:17.082: W/System.err(24259): at java.lang.reflect.Method.invoke(Method.java:515)
09-17 01:03:17.082: W/System.err(24259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
09-17 01:03:17.082: W/System.err(24259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
09-17 01:03:17.082: W/System.err(24259): at dalvik.system.NativeStart.main(Native Method)
other php codes:
<?php
class DB_Functions {
private $db;
function __construct() {
require_once 'DB_Connect.php';
$this->db = new DB_Connect();
$this->db->connect();
}
function __destruct() {
}
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"];
$salt = $hash["salt"];
$result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
if ($result) {
$uid = mysql_insert_id();
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
return mysql_fetch_array($result);
} else {
return false;
}
}
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
if ($encrypted_password == $hash) {
return $result;
}
} else {
return false;
}
}
public function isUserExisted($email) {
$result = mysql_query("SELECT email from users WHERE email = '$email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
return true;
} else {
return false;
}
}
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
<?php
class DB_Connect {
function __construct() {
}
function __destruct() {
}
public function connect() {
require_once 'include/Config.php';
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_select_db(DB_DATABASE) or die(mysql_error());
return $con;
}
public function close() {
mysql_close();
}
}
?>