我正在尝试使用PHP和JSON在MySQL Google地图活动中添加来自MySQL的数据。
这是我的PHP脚本:
<?php
mysql_connect("localhost","root","");
mysql_select_db("TripTun");
mysql_query("SET NAMES 'utf8'");
$sql=mysql_query("select * from etablissement");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print (json_encode($output)) ;
mysql_close();
?>
这是执行php脚本后的json结果
[{"idet":"1","lang":"36.703816","latt":"10.406306"},{"idet":"2","lang":"36.707162","latt":"10.408623"},{"idet":"3","lang":"36.711325","latt":"10.415039"}]
这是我在地图活动中的Java代码:
package com.example.el_boj.triptun;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Map extends FragmentActivity {
private GoogleMap mMap;
private static LatLng BORJ_CEDRIA = new LatLng(36.705657, 10.406992);
private static final String LOG_TAG = "TripTun/map.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.act_map);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) { mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(BORJ_CEDRIA,15));
new Thread(new Runnable() {
public void run() {
try {
retrieveAndAddCities();
} catch (IOException e) {
Log.e(LOG_TAG, "Cannot retrive cities", e);
}
}
}).start();
}
protected void retrieveAndAddCities() throws IOException {
InputStream is = null;
String result = "";
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.3.3/TripTun/map.php");
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());
}
try{
assert is != null;
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
result = sb.toString().substring(0, sb.toString().length()-1);
}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
mMap.addMarker(new MarkerOptions().position(new LatLng(
jsonObj.getJSONArray("lang").getDouble(0),
jsonObj.getJSONArray("latt").getDouble(1)
))
);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
}
我收到此错误:
03-12 15:42:07.983 2358-2410/com.example.el_boj.triptun E/TripTun/map.php﹕ Error processing JSON
org.json.JSONException: Unterminated array at character 153 of [{"idet":"1","lang":"36.703816","latt":"10.406306"},{"idet":"2","lang":"36.707162","latt":"10.408623"},{"idet":"3","lang":"36.711325","latt":"10.415039"}
at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
at org.json.JSONTokener.readArray(JSONTokener.java:440)
at org.json.JSONTokener.nextValue(JSONTokener.java:103)
at org.json.JSONArray.<init>(JSONArray.java:92)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.example.el_boj.triptun.Map.retrieveAndAddCities(Map.java:129)
at com.example.el_boj.triptun.Map$1.run(Map.java:94)
at java.lang.Thread.run(Thread.java:818)