Orgil
Orgil

Reputation: 711

How to retrieve data using json into mysql android

I want to retrieve data from user table using username but it cannot work properly here is my php code:

<?php

mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxx");

$username = $_POST["name"];

$sql = mysql_query("select * from user where username = '$username'");

$flag["code"] = 0;

$check = mysql_fetch_row($sql);
if ($check > 0)
{
    $flag["code"] = 1;
    echo json_encode($flag);    

    while ($row= mysql_fetch_assoc($sql)) 
    {
        $output[] = $row;
    }
}

echo json_encode($output);
mysql_close();

?>

And here is my Java code:

  try
  {
     JSONObject json = new JSONObject(result);
     code = (json.getInt("code"));

if (code == 1)
{
    JSONArray array = new JSONArray(result);

    for (int i = 0; i < array.length(); i++)
    {
        JSONObject jsonObject = array.getJSONObject(i);
        firstname = jsonObject.getString("firstname");

        et.setText(firstname);
    }
}
}
catch (Exception e)
{

}

Here is result:

try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8),8);
            StringBuilder sb = new StringBuilder();

            while ((line = reader.readLine()) != null)
            {
                sb.append(line+"\n");
            }

            is.close();

            result = sb.toString();

            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

There is no error but it cannot retrieve any data what is the problem ?

Upvotes: 0

Views: 588

Answers (2)

Orgil
Orgil

Reputation: 711

I've just solved the problem result didn't work in other words it cannot get data by array and i just put array function now it works fine :-)

Upvotes: 0

Sagar Devkota
Sagar Devkota

Reputation: 1192

Please return json object from php not Array i have channged echo part to json_encode(array('response'=>$output,'code'=>$flag["code"]));

<?php

mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxx");

$username = $_POST["name"];

$rs = mysql_query("select * from user where username = '$username'");


$flag["code"] = 0;

$check = mysql_num_rows($rs);
if ($check > 0)
{
    $flag["code"] = 1;


    while ($row= mysql_fetch_array($rs)) 
    {
        $output[] = $row;
    }
}

echo json_encode(array('response'=>$output,'code'=>$flag["code"])); //changed here
mysql_close();

?>

Now in java,

try
  {
     JSONObject json = new JSONObject(result);
     code = (json.getInt("code"));

if (code == 1)
{
    JSONArray array = json.getJSONArray('response');

    for (int i = 0; i < array.length(); i++)
    {
        JSONObject jsonObject = array.getJSONObject(i);
        firstname = jsonObject.getString("firstname");

        et.setText(firstname);
    }
}
}
catch (Exception e)
{

}

If it fails JSONObject json = new JSONObject(result); is not required because result is already json object. just remove that line and change all json variables to result.

Upvotes: 1

Related Questions