Dear All
I am getting problem in PHP code but I don't know also Android code is correct or not. Anyone help me to find out the problem.
As below I am showing my written code.
MainActivity.Java Code
public class MainActivity extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 2000;
protected LocationManager locationManager;
protected Button retrieveLocationButton;
TextView txtLatLng;
UserClass user;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
txtLatLng = (TextView) findViewById(R.id.textview1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
new HttpAsyncTask().execute("http://localhost:8080/Monitoring/index.php");
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
String message = String.format("Current Location \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message,Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format("New Location \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(), location.getLatitude());
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MainActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,"GPS turned off disabled by the user. ",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,"GPS turned on enabled by the user. ",Toast.LENGTH_LONG).show();
}
}
public static String POST(String url, UserClass user){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("Lat", user.getLat());
jsonObject.accumulate("Lon", user.getLon());
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
;
httpPost.getParams().setParameter("jsonpost",json);
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
user = new UserClass();
user.setLat(location.getLatitude());
user.setLon(location.getLongitude());
return POST(urls[0],user);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this,"Data Sent Successfully !!!", Toast.LENGTH_LONG).show();
}
}
}
PHP Code
<?php
echo 'Hello, world!';
$json = $_GET['json'];
$data = json_decode($json);
print_r($data);
$tabid = $data->tabid;
$lon = $data->lon;
$lat = $data->lat;
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("dssmonitoring", $con);
echo "tabid" +$tabid;
echo "lat" + $lat;
echo "lon" + $lon;
$sql = "INSERT INTO `dssmonitoring`.`monitor` (
`tabid`,
`lat`,
`lon`
) VALUES (
'$tabid',
'$lat',
'$lon'
);";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
mysql_close($con);
?>