0

here is my php code

$titikPetaInti = array();
while($row = mysql_fetch_assoc($hasil2))
{
     $titikPetaInti[] = $row['koordinat'];
}

$data = "{titikPeta:".json_encode($titikPetaInti)."}";
echo $data;
?>

then here is my android code xResultTitikPeta is result request to php

jObject = new JSONObject(xResultTitikPeta);
        JSONArray myArray1 = (JSONArray) jObject.getJSONArray("titikPeta");
        String[]titikPeta = new String[myArray1.length()];

        for(int a = 0; a < myArray1.length(); a++)
        {
            titikPeta[a] = myArray1.getJSONObject(a).toString();
        }
        teks1 = (TextView) findViewById(R.id.textView1);
        teks1.setText(Arrays.toString(titikPeta));

it displaying null at emulator like no value

--EDIT--

i think there something mistake in parsing code, cus when i display the xResultTitikPeta in android, it give me string result

here is result of xResultTitikPeta

{titikPeta:["-8.705378,115.225189","-8.56056700000000,115.42395100000","-8.57659700000000,115.40065300000","-8.55596300000000,115.41085700000","-8.51855200000000,115.491908000000","-8.54743200000000,115.41036800000","-8.56551100000000,115.45173900000","-8.44321000000000,115.616019000000"]}
6
  • are you sure if $row['koordinat'] is settign a value? Commented Jun 13, 2012 at 1:19
  • @Sana, yes. and i have tried the php code directly and it give me the result, but when it parse to android, they return null value Commented Jun 13, 2012 at 1:22
  • Can you show us the xResultTitikPeta string? Have you run it through a site like JSONLint to check the syntax? Commented Jun 13, 2012 at 2:30
  • @Ken, yes the result is string Commented Jun 13, 2012 at 2:38
  • one can test the code here but with some includes also.. Also this question needs some java guys, but question has not that tag. Commented Jun 13, 2012 at 2:46

3 Answers 3

1

this is malformed JSON! no double quotes on key.

$data = "{titikPeta:".json_encode($titikPetaInti)."}";

instead do:

$data = '{"titikPeta":'.json_encode($titikPetaInti).'}';

EDITED:

Ok, remove that hand made approach:

$data = json_encode(array("titikPeta"=>$titikPetaInti));
Sign up to request clarification or add additional context in comments.

Comments

0

OK, I've found your bug! As well as fixing the $data = json_encode(array("titikPeta" => $titikPetaInti)); issue, the problem is here:

titikPeta[a] = myArray1.getJSONObject(a).toString();

The elements of myArray1 are actually of type string and cause an exception to be thrown, so you need instead:

titikPeta[a] = myArray1.getString(a);

This produces the output of:

[-8.705378,115.225189, -8.56056700000000,115.42395100000, -8.57659700000000,115.40065300000, -8.55596300000000,115.41085700000, -8.51855200000000,115.491908000000, -8.54743200000000,115.41036800000, -8.56551100000000,115.45173900000, -8.44321000000000,115.616019000000]

As each element in your array is of the form "-8.705378,115.225189", the JSON parser assumes they are strings. If you change the elements to "-8.705378","115.225189" you can also use:

titikPeta[a] = Double.toString(myArray1.getDouble(a));

However, the first version will work too.

Note: my personal preference is that I would declare each array element as:

{"x":-8.705378,"y":115.225189}

Comments

0

try

$data = "{\"titikPeta\":".json_encode($titikPetaInti)."}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.