I am a beginner in programming and I just want to create an app that display some markers on map using some coordinates from a localdatabase (MySQL), using PHP and JSON.The map is working, but there are no markers. Please help me because I really cannot figure it out.
This is the error i get:
Error processing JSON
org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at org.json.JSONArray.(JSONArray.java:92)
at org.json.JSONArray.(JSONArray.java:108)
at com.kid.compaq.licenta.MapsActivity$MarkerTask.onPostExecute(MapsActivity.java:108)
at com.kid.compaq.licenta.MapsActivity$MarkerTask.onPostExecute(MapsActivity.java:62)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
This is my MapActivity
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
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;
import java.net.HttpURLConnection;
import java.net.URL;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
new MarkerTask().execute();
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().isCompassEnabled();
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setScrollGesturesEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(true);
}
class MarkerTask extends AsyncTask<Void, Void, String> {
public static final String LOG_TAG = "ExampleApp";
public static final String SERVICE_URL = "http://192.168.48.14/stations.php";
// Invoked by execute() method of this object
@Override
protected String doInBackground(Void... args) {
HttpURLConnection conn;
InputStream in = null;
String result = "";
try {
URL url = new URL(SERVICE_URL);
conn = (HttpURLConnection) url.openConnection();
conn.connect();
in = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
assert in != null;
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
result += line;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(String result) {
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
LatLng latLng = new LatLng(jsonObj.getJSONArray("latlng").getDouble(0),
jsonObj.getJSONArray("latlng").getDouble(1));
// Create a marker for each station in the JSON data.
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title(jsonObj.getString("nume"))
.position(latLng));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
}
}
This is my php script
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
@$dbhandle = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");
$selected = mysql_select_db("incercare",$dbhandle)or die("Could not select licenta");
$sql="SELECT id_st, nume, CONCAT_WS(\",\",lat, lng) AS latlng FROM statii";
$result = mysql_query($sql);
$response= array();
while($row= mysql_fetch_assoc($result)){
$response[]=array(
'nume' => $row['nume'],
'latlng' => explode(',', $row['latlng']),
'id_st' => $row['id_st']
);
}
$bd_json = json_encode($response);
echo json_encode($response);
?>
and this is my json output
[{"nume":"Merge in sfarsit","latlng":["45.659721","25.606859"],"id_st":"5"},{"nume":"Memo Cantina","latlng":["45.655075","25.581560"],"id_st":"6"}]
Any idea on what is wrong and what must be done in order to make it work?