Reputation: 1441
My PHP file:
<?php
include("ConnectDatabase.php");
$Username = mysql_real_escape_string($_POST['Username']);
$Password = mysql_real_escape_string($_POST['Password']);
$q = mysql_query("SELECT Username, Password FROM Users
where Username = '".$Username."' and
Password = '".$Password."'", $con);
if(mysql_num_rows($q) > 0){
$row = mysql_fetch_assoc($q);
print json_encode($row);
}else{
print "0";
}
?>
I tried to parse that by the following to get a value,but it got null values both userJson and passJson:
public void parseJson(String result){
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
userJson = jArray.getJSONObject(i).getString("Username").toString();
passJson = jArray.getJSONObject(i).getString("Password").toString();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
Anyone can see my mistake ? Thank you
PS This is my older post that related to this post.
Upvotes: 0
Views: 606
Reputation: 326
I would think they end up null cuz the first line doesn't work. It's not a JSONArray as you have generated.
try {
JSONObject root = new JSONObject(result);
username = root.getString("Username");
password = root.getString("Password");
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
something like that.
Upvotes: 3