0

I am trying to get data from a php file.

This is my PHP code:

$sql=mysql_query("select * from `tracking`");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));

And I really need all the data. This is the code I use to get and convert the data:

String result = null;
InputStream is = null;
StringBuilder sb = null;

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://server/getData.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
} catch(Exception e) {
    Log.e("log_tag", "Error in http connection"+e.toString());
}

//convertion de la réponse en String
try {
    //BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
    sb = new StringBuilder();
    sb.append(reader.readLine() + "\n");

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

    is.close();
    result=sb.toString();             
} catch(Exception e) {
    Log.e("log_tag", "Error converting result "+e.toString());
}

// Affectation des données
try {
    JSONArray jArray = new JSONArray(result);
    Log.d("jArray", ""+jArray);
    JSONObject json_data= null;

    for(int i=0; i<jArray.length(); i++) {
        p = new Position();
        json_data = jArray.getJSONObject(i);

        Log.d("js", ""+json_data);
        id=json_data.getInt("id");
        lat=(float) json_data.getDouble("lat");
        lon=(float) json_data.getDouble("lon");
        speed=(float) json_data.getDouble("speed");
        alt=json_data.getDouble("alt");
        time=json_data.getString("time");
        date=json_data.getString("date");

        p.setId(id);
        p.setLat(lat);
        p.setLon(lon);
        p.setSpeed(speed);
        p.setAlt(alt);
        p.setDate(date);
        p.setTime(time);

        datasource.createPosition(p);
    }
} catch(JSONException e1) {
    Log.e("JSONEX", ""+e1);
} catch (ParseException e1) {
    e1.printStackTrace();
}

This really works on emulator nicely but not on my android phone and I don't get the reason.
Now this is my json result from the server:

[{"id":"180","lat":"33.894707","lon":"-6.327312","speed":"0.00000","alt":"397","date":"29/07/2012","time":"23:44"}]

It's a valid JSONArray validated on http://jsonlint.com/

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
TheSM
  • 59
  • 1
  • 13
  • JSONExeption: value of type Java.lang.String cannot be converted to JSONArray. NB: on the emulator I get everything work but on the phone I get the exception. – TheSM Jul 31 '12 at 15:45
  • Is there someway to get the data from **result** without passing by **JSONArray** ? I need to make it work soonely and I was shocked when it didn't work on the phone :/ – TheSM Aug 01 '12 at 14:39
  • You may try using [Gson](http://code.google.com/p/google-gson/). Its one of the best tool in parsing Json strings into objects and vice versa. – waqaslam Aug 01 '12 at 15:07

2 Answers2

2

I found what was wrong with JSON. I found at the beginning a character "?" added somewhere in the code and it was not visible until I stored the logCat entry to a file. So I added the code line and it works fine now.

result = result.substring(1);

I hope it may help someone in the future thanx to Waqas

TheSM
  • 59
  • 1
  • 13
  • Just wanted to thank you for this, it solved my problem. I want to stress this bug did not happen on Android v 4.* , only on Android v 2.* . Also The json file I am using is local, from my assets folder, and its content is static. Really weird – Don Mar 11 '13 at 19:12
  • I still don't understand why this happend very weired anyway we have now a way to bypass it. don't forget to vote it as an answer so it's recommanded to others – TheSM Mar 11 '13 at 22:47
0

Put this lines:

        while(result.charAt(0)!='[')  //or result.charAt(0)!='{'
        {
            result = result.substring(1);
            Log.d("Tag1", "remove 1st char");

        }

after:

        result = sb.toString();

To become like this:;

        result = sb.toString();

        while(result.charAt(0)!='[')
        {
            result = result.substring(1);
            Log.d("Tag1", "remove 1st char");

        }