Im trying to download Latitude and longitude data from my database, and create map markers in android for them. However i am receiving an error
05-25 18:54:33.834: E/ExampleApp(4893): Error processing JSON
05-25 18:54:33.834: E/ExampleApp(4893): org.json.JSONException: Value 51.9111546,4.477839 at latlng of type java.lang.String cannot be converted to JSONArray
05-25 18:54:33.834: E/ExampleApp(4893): at org.json.JSON.typeMismatch(JSON.java:100)
05-25 18:54:33.834: E/ExampleApp(4893): at org.json.JSONObject.getJSONArray(JSONObject.java:553)
05-25 18:54:33.834: E/ExampleApp(4893): at info.androidhive.jsonparsen.mapview.createMarkersFromJson(mapview.java:117)
05-25 18:54:33.834: E/ExampleApp(4893): at info.androidhive.jsonparsen.mapview$2.run(mapview.java:99)
05-25 18:54:33.834: E/ExampleApp(4893): at android.os.Handler.handleCallback(Handler.java:733)
05-25 18:54:33.834: E/ExampleApp(4893): at android.os.Handler.dispatchMessage(Handler.java:95)
05-25 18:54:33.834: E/ExampleApp(4893): at android.os.Looper.loop(Looper.java:136)
05-25 18:54:33.834: E/ExampleApp(4893): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-25 18:54:33.834: E/ExampleApp(4893): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 18:54:33.834: E/ExampleApp(4893): at java.lang.reflect.Method.invoke(Method.java:515)
05-25 18:54:33.834: E/ExampleApp(4893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-25 18:54:33.834: E/ExampleApp(4893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
05-25 18:54:33.834: E/ExampleApp(4893): at dalvik.system.NativeStart.main(Native Method)
I cannot seem to find how to fix this
Here is my activity code (apart from some SetUpMap methods).
protected void retrieveAndAddCities() throws IOException {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
try {
// Connect to the web service
URL url = new URL(SERVICE_URL);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Read the JSON data into the StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
json.append(buff, 0, read);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to service", e);
throw new IOException("Error connecting to service", e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
// Create markers for the city data.
// Must run this on the UI thread since it's a UI operation.
runOnUiThread(new Runnable() {
public void run() {
try {
createMarkersFromJson(json.toString());
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
});
}
void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each city in the JSON data.
JSONObject jsonObj = jsonArray.getJSONObject(i);
map.addMarker(new MarkerOptions()
.title(jsonObj.getString("name"))
.snippet(Integer.toString(jsonObj.getInt("population")))
.position(new LatLng(
jsonObj.getJSONArray("latlng").getDouble(0),
jsonObj.getJSONArray("latlng").getDouble(1)
))
);
}
}
}
the JSON responce
[{"name":"AlbertHeijn","latlng":"51.9111546,4.477839","population":"1234"},{"name":"Jumbo","latlng":"51.9054127,4.4960587","population":"23"}]
I am using the json_encode($resultarray)
to show my JSON on the website.
any detailed explanation wil be greatly appreciated.