I m trying to do a registration page from an android activity connectiong the datas to my sqldatabase, I m getting this error \" org.json.JSONException: Value
First of
I have the same problem; i found the solution the easy way.
In Java code just type Log.e("anyText",response);
In logCat it will show you what is problem
Thanks I manage to correct some of the errors 2 ';' were missing and I could see the error with Log.i("tagconvertstr", "["+result+"]");
The msg that it is showing is something like that :
[<br/ > font size = 1 table class=''xdebug-erroe' dir='ltr' ... loads of html code that wasn't in my initial code then....{"message":"Problem sending you the activation mail !"}]
So there's a problem with the json format on that message "Problem sending you the activation mail" but the user was registered OK!
So the second time i would try that code it would show me in a correct json format : "This email has already been used" ! (without any errors) but i still can't find the error in my php code : S
You should set Content-Type
header before sending back the response.
header('Content-Type: application/json');
print(json_encode(array("message" => $msg)));
Check this : Returning JSON from a PHP Script
try preventing any output before your print and change the print
to echo
you may also remove the closing ?>
to prevent further output after the php script
<?php
ob_start();
if( isset($_POST['login']) && isset($_POST['pwd']) && isset($_POST['name']) && isset($_POST['firstname'])) {
include("connexion_bdd.php");
if(connexionBDD() == 1){
$login = $_POST['login'];
$pwd = $_POST['pwd'];
$name = $_POST['name'];
$firstname = $_POST['firstname'];
$sql = "SELECT colUserID
FROM userTable
WHERE colUserLogin = '".$login."' ";
$req = mysql_query($sql);
$resultat=mysql_num_rows($req);
if($resultat==0){
$temps = time();
$clef = md5($login . $temps);
$req = mysql_query("INSERT INTO userTable(colUserLogin, colUserPwd, colUserName, colUserFirstname, colUserKey, colUserDate)
VALUES( '$login', '$pwd', '$name', '$firstname', '$clef', '$temps')");
if($req){
$destinataire = $login;
$sujet ="Welcome on SnSR";
$from = "From: SpotnShareReminder@live.com \r\n";
$from .= "Content-Type: text/html; charset=us-ascii\r\n";
$message = ' Clic on the link below :<br/>
<a href="http://localhost/spotnshare/validation_mail.php?usrk='.$clef.' ">
Registration confirmation.
</a> ';
ini_set('SMTP','relay.skynet.be');
if(mail($destinataire,$sujet,$message,$from)){
$msg = 'Check your mailbox to activate your account !';
}
else{
$msg = 'Problem sending you the activation mail !';
$req = mysql_query("DELETE FROM userTable WHERE colUserLogin = '".$pseudo."' ");
}
}
else{
$msg = 'Problem inserting you in our database !';
}
}else{
$msg = 'This email has already been used !';
}
mysql_free_result ($req);
}else{
$msg = "Connexion problem with de DB"
print(json_encode(array("message" => $msg)));
}
}else{
$msg = "Couldn't treat your datas"
}
ob_end_clean()
echo(json_encode(array("message" => $msg)));
Your request to http://192.168.1.101/spotnshare/subscribe.php is failing and returning a non-JSON string (probably a PHP error). You can print out the value with a
Log.i("tagconvertstr", "["+result+"]");
before the new JSONObject
call to see what you're getting before parsing it.
EDIT: if you are using Eclipse you can set a break point and step through to see what's going on.