Connection error with android php? (add java code)

I am trying to get location (latitude, longitude) from android app and insert into database using php and then fetch data within 10m radius.

The problem is that when I test the code with a smartphone (local test is fine) the data is not inserted correctly. The "usergps" table has 3 columns (name, latitude, longitude), and after I checked the code, (, 0, 0) is inserted.

Can you check the code below? I have added 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 gets 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 the PHP code is here. Php to android connection error? (+ MySQL)

I cannot check the error in the logcat. I have to check this using my 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);
                        ...

      

+3


source to share


1 answer


in your:

private void sendGps(final Double lat, final Double lon, final String name) 

      

you are not sending parameters (latin name name) to your PHP server. I'm not very familiar with PHP, but I guess since these parameters are not included in your POST, php uses the defaults: "" for String and 0 for numbers.

to send the data needed to change the request from StringRequest to JsonObjectRequest



so your code looks something like this:

HashMap<String, String> params = new HashMap<String, String>();
params.put("lat", ""+lat);
params.put("lon", ""+lon);
params.put("name", name);

JsonObjectRequest strReq = new JsonObjectRequest(
          AppConfig.URL_GPS, new JSONObject(params), new Response.Listener<String>() {

      

remember you need to change your response listener because you are now getting the response of a json object.

0


source







All Articles