Android app submit button not working on sending gps data to mysql server

I’m trying to create a simple app that submits GPS data to a website MySql server but when I added the code for getting the GPS location the submit button stopped working

Any help would be greatly appreciated

public class MainActivity extends AppCompatActivity {

EditText inputName;
String currentTimeDate;
TextView timeView, latView, lonView;
double longitude, latitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnInsert = (Button) findViewById(R.id.pushData);
    inputName = (EditText) findViewById(R.id.nameInput);
    latView = (TextView) findViewById(R.id.latView);
    lonView = (TextView) findViewById(R.id.lonView);

    timeView = (TextView) findViewById(R.id.timeView);
    currentTimeDate = DateFormat.getDateTimeInstance().format(new Date());
    timeView.setText(currentTimeDate);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);

    btnInsert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new InsertAsync().execute("http://androidemployeelog.eu/php-service/dbsave.php");
        }
    });


}

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();

        String lat = Double.toString(latitude);
        String lon = Double.toString(longitude);

        latView.setText(lat);
        lonView.setText(lon);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    public void onProviderEnabled(String provider) {

    }

    public void onProviderDisabled(String provider) {
    }
};

class InsertAsync extends AsyncTask<String, Void, Void> {


    String name = inputName.getText().toString();
    String phoneTime = currentTimeDate.toString();
    String lat = Double.toString(latitude);
    String lon = Double.toString(longitude);
    ProgressDialog progressDialog;
    String isSucess = "0";



    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setTitle("Logging Information");
        progressDialog.setIndeterminate(false);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(String... params) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormEncodingBuilder()
                .add("phoneTime", phoneTime)
                .add("phoneLat", lat)
                .add("phoneLon", lon)
                .add("nameInput", name)
                .build();

        Request request = new Request.Builder().url(params[0])
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String result = response.body().string();

            JSONObject jsonObject = new JSONObject(result);

            isSucess = jsonObject.getString("success");

            Log.d("isSuccess", isSucess);

        } catch (Exception ex) {
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        progressDialog.dismiss();
        inputName.setText("");

        inputName.hasFocus();

        if (isSucess.equals("1")) {
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, "Submission successful", duration);
            toast.show();
        }
        if (isSucess.equals("0")) {
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, "Submission failed, please check your internet connection and try again", duration);
            toast.show();
        }
    }
}

}

Leave a Reply

avatar
  Subscribe  
Notify of