I'm trying to get location(latitude, longitude) from android application and insert into database using php then extract data within a 10m radius.
The Problem is, when I test the code using smartphone (local test is OK), data is not correctly inserted. Table 'usergps' has 3 columns (name, latitude, longitude) and after I test the code, ( , 0, 0) is inserted.
Can you check the code below? I added the java code.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getgps);
public void onClick(View view) {
gps = new GpsActivity(getGpsValues.this);
//this is new part
Intent intent = getIntent();
// Receiving the Data from MainActivity
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
String email = user.get("email");
if(gps.canGetLocation()){
double lat = gps.getLatitude();
double lon = gps.getLongitude();
sendGps(lat,lon,name);
}
HashMap<String, String> gps = db.getNumbers();
String num = gps.get("num");
} }
private void sendGps(final Double lat, final Double lon, final String name) {
// Tag used to cancel the request
String tag_string_req = "req_gps";
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_GPS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "GPS Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// GPS successfully stored in MySQL
// Now store the GPS in sqlite
JSONObject gps = jObj.getJSONObject("gps");
Double lat = gps.getDouble("lat");
Double lon = gps.getDouble("lon");
String name = gps.getString("name");
// Inserting row in users table.
db.addGps(lat, lon, name);
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
this part is getting data from php. this is another class.
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
public HashMap<String, String> getNumbers() {
HashMap<String, String> gps = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_GPS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
gps.put("num", cursor.getString(1));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + gps.toString());
return gps;
}
and PHP code is here. Android php connection error? (+mysql)
I can't check error in logcat. I have to check this by using smartphone. Please check the code and let me know.
HashMap<String, String> params = new HashMap<String, String>();
params.put("lat", ""+lat);
params.put("lon", ""+lon);
params.put("name", name);
JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST,
AppConfig.URL_GPS, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "GPS Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject();
boolean error = jObj.getBoolean("error");
if (!error) {
JSONObject gps = jObj.getJSONObject("gps");
String name = gps.getString("name");
Double lat = gps.getDouble("lat");
Double lon = gps.getDouble("lon");
// Inserting row in users table.
db.addGps(name, lat, lon);
...