0

I keep receiving this error and I know why but cannot seem to fix it, i've tried to implement a fix for it but it seems to have done nothing even though posts have said this should fix it, i retrieve data from a PHP script which is json encoded, and then try iterate through it to store it an array list .

setlat(), set lng() and setmsg() come from a constructor class

PHP script

<?php
$hostname_localhost ="**";
$database_localhost ="**";
$username_localhost ="**";
$password_localhost ="**";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or    trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$sql="SELECT latitude,longitude,message FROM locations";
$result=mysql_query($sql);

while ($row=mysql_fetch_assoc($result) or die(mysql_error())) 
{
 $output []= $row;
 print(json_encode($output));
}
mysql_close($localhost);
?>

what the script produces

[{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"}][{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"},{"latitude":"54.01182883490973","longitude":"-2.7903684228658676","message":"freddy"}]

my http post

 public String getdata(){
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("******");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);
        Toast.makeText(Reminders.this, "response" + response, Toast.LENGTH_LONG).show();
        return  response.trim();
     }  catch (Exception e){
        System.out.print("Your exception : " + e);
        return "invalid";

this is where the methods are called

 new Thread(new Runnable() {
                public void run() {
                    String result = getdata();
                    ArrayList<locations> lcd = parseJSON(result);

                }
            }).start();

the error occured

public ArrayList<locations> newdata = new ArrayList<locations>();
    try {
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            locations llm = new locations();
            llm.setlat(json_data.getString("latitude"));
            llm.setlng(json_data.getString("longitude"));
            llm.setmsg(json_data.getString("message"));
            newdata.add(llm);
       }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return newdata;
}

I am receiving an error on JSONObject json_data The error

Error parsing data org.json.JSONException: Value invalid of type java.lang.String cannot be converted to JSONArray
Harjit
  • 11
  • 3
  • 4
    post your `JSON` data. – Yugesh Mar 19 '15 at 09:30
  • possible duplicate of [Convert string to JSON array](http://stackoverflow.com/questions/15609306/convert-string-to-json-array) – ewan.chalmers Mar 19 '15 at 09:38
  • As @Yugesh said without showing the json format how we can help you? – Remees M Syde Mar 19 '15 at 09:50
  • 1
    show me your json data, I think there are two possible reason for your question. First, you have a wrong format of json. Second, encoding errors. – SilentKnight Mar 19 '15 at 09:53
  • @SilentKnight , i edited the post i hope thats what you requested – Harjit Mar 19 '15 at 09:57
  • What you said as `{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"}][{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"},{"latitude":"54.01182883490973","longitude":"-2.7903684228658676","message":"freddy"}]` is not valid JSON – EpicPandaForce Mar 19 '15 at 10:00

3 Answers3

1

The format of the result didn't match the format of an JsonArray. The result indicates that there is just one JsonObject which contains two JsonArray but not one. So, the reason is that you got a wrong JsonArray in wrong format. I think the right JsonArray format should be like this:

[{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"},{"latitude":"54.009222844372566","longitude":"-2.787822335958481","message":"jamie"},{"latitude":"54.01182883490973","longitude":"-2.7903684228658676","message":"freddy"}]

Look deep into the difference between the string and yours. Then you will get the right JsonArray parsing.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • i see what you mean, how would i format it? ive tried using JSON_PRETTY_PRINT but hasnt formatted it correctly @SilentKnight – Harjit Mar 19 '15 at 10:15
  • you need to modify your PHP codes. Your PHP didn't handle with result in a right format. – SilentKnight Mar 19 '15 at 10:20
0

I assume the error occurs at this line:

JSONArray jArray = new JSONArray(result);

Assuming that result is a String, I think you need to load the String into a JSONObject first and retrieve the Array from that, e.g.

JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("xxx");

... where xxx is the name of an Array variable in your JSON string.

ewan.chalmers
  • 16,145
  • 43
  • 60
0
JSONObject obj1 = new JSONObject(resultSTR);
 try {
  JSONArray result = obj1.getJSONArray("xxx");
for(int i=0;i<=result.length();i++)
 {
        locations llm = new locations();
        llm.setlat(result.getString("latitude"));
        llm.setlng(result.getString("longitude"));
        llm.setmsg(result.getString("message"));
        newdata.add(llm);
 }
 } catch (JSONException e) {
     e.printStackTrace();
}

Try this

I hope this help you

Bhagirathsinh Gohil
  • 673
  • 1
  • 9
  • 25