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