0

I'm having a problem parsing the JSON response from MySQL in Java.

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://parkfinder.zxq.net/default.php");
    httppost.setEntity(new UrlEncodedFormEntity(coordinatesToSend));
    HttpResponse response = httpclient.execute(httppost);
    Log.d("HTTP Client", "HTTP Request made");

    HttpEntity entity = response.getEntity();
    inputStream = entity.getContent();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,
            "iso-8859-1"), 8);
    sb = new StringBuilder();
    sb.append(bufferedReader.readLine() + "\n");

    String line = "0";
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line + "\n");
    }
    inputStream.close();
    bufferedReader.close();
    result = sb.toString();
    Log.d("RESULT", result);
    JSONObject json_data = new JSONObject(result);
    Log.d("JSON","Finished");
    JSONArray nameArray = json_data.names();
    JSONArray valArray = json_data.toJSONArray(nameArray);
    for (int i = 0; i < nameArray.length(); i++) {
        Log.d("NAMES", nameArray.getString(i));
    }
    for (int i = 0; i < nameArray.length(); i++) {
        Log.d("NAMES", nameArray.getString(i));
    }

} catch (Exception e) {
    // TODO: handle exception
}

This is the MySQL Accessing and retreiving info, and parsing it afterwars. the

Log.d("RESULT", result);

line posts the correct results:

2[{"longtitude":"32.32","latitude":"33.12"}]

however the

Log.d("JSON","Finished");

Never gets called, so the problem seems to be on this line

JSONObject json_data = new JSONObject(result);

This while thing is taken from a tutorial which I saw many examples of it over the internet and on this site, some stated errors, but not this one.

Any help would be great! Thanks

EDIT: The printStackTrace() output:

0`5-14 21:38:18.639: WARN/System.err(665): org.json.JSONException: A JSONObject text must begin with '{' at character 1 of 2[{"longtitude":"32.32","latitude":"33.12"}]`

The php code:

<?php
$host = "localhost";
$user = "**MASKED**";
$password = "**MASKED**";
$database = "parkfinder_zxq_coordinates";
$connection = mysql_connect($host, $user, $password) or die("couldn't connect to server");
$db = mysql_select_db($database, $connection) or die("couldn't select database.");
//$request_parked = $_REQUEST['parked'];
$request_long = $_REQUEST['longtitude'];
$request_lat = $_REQUEST['latitude'];
//if ($request_parked == 'FIND') {
$q = mysql_query("SELECT * FROM Coordinates");
while ($e = mysql_fetch_assoc($q))
    $output[] = $e;

print (json_encode($output));
//}

mysql_close();
?>
wattostudios
  • 8,666
  • 13
  • 43
  • 57
La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • 6
    So most likely the parse is failing, throwing an exception, and your exception handler is blank, so nothing happens... finish off that "TODO" and at least have the catch block spit out the exception error text. – Marc B May 23 '12 at 14:07
  • add e.printStackTrace(); to your `catch` block – Alex Stybaev May 23 '12 at 14:10

1 Answers1

2

Your JSON data 2[{"longtitude":"32.32","latitude":"33.12"}] isn't valid (the number 2 isn't proper JSON syntax).

Can I suggest you actually mean

[{"longtitude":"32.32","latitude":"33.12"}]`

(ie. without the 2 at the beginning)

You can use the validator at http://jsonlint.com/ to check your JSON code.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • I added the PHP code, I havn't done any modification to the output one obtained from the mysql – La bla bla May 23 '12 at 14:14
  • Well the `2` is where your exception trace is saying the JSON code is invalid, and it definitely doesn't parse at http://jsonlint.com/ . Where is the `2` coming from - is it returned from your MySQL or is it something from your HTML connection? I will suggest maybe MySQL is correct but your HTML reading code is adding the 2 for some reason? – wattostudios May 23 '12 at 14:17
  • what i pasted, is the entire php file. it doesn't any have html interface, it only use for interaction with the device. any idea what's wrong with the php? – La bla bla May 23 '12 at 14:19
  • I don't think there's anything wrong with your PHP, I think its something wrong with your Java code. – wattostudios May 23 '12 at 14:20
  • 1
    Instead of using BufferedReader to read the HttpEntity content, why don't you try getting rid of all your manual reading code and just use `String result = EntityUtils.toString(entity);` - it does all the hard work of reading the content for you. It might help to remove a potential source of error. Refer to http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html for details of this class. – wattostudios May 23 '12 at 14:25
  • Should I ask a new question regarding the wrong JSON output, or edit this one? – La bla bla May 23 '12 at 14:58
  • Hmm, not sure where the 2 is coming from. Maybe look at the answer http://stackoverflow.com/questions/383631/json-encode-mysql-results which says that the PHP code should have a `$e = array();` line before the `while` statement - perhaps if it doesn't know that it is an array, it might not be formatting the output properly? – wattostudios May 24 '12 at 02:43