Как отправить двойной с залпом в андроид студии - PullRequest
0 голосов
/ 07 мая 2019

В настоящее время я делаю приложение для Android, и мне нужно отправить locationt в базу данных. Вот код в соединении дисплея:

private void displayLocation() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            latitude = mLastLocation.getLatitude();
            longitude = mLastLocation.getLongitude();
            txtCoordinates.setText(latitude + " / " + longitude);
        } else
            txtCoordinates.setText("Couldn't get the location. Make sure location is enable on the device");

    }

У меня нет проблем с получением значений, но проблема в том, что я пытаюсь отправить их в базу данных. Вот мой залп:

private void EnvioGPS() {
        //final String relatorio = this.relatorio_text.getText().toString().trim();
       final String longitude = this.longitude2.getBytes().toString().trim();
       final String latitude = this.latitude2.getBytes().toString().trim();


        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_GPS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try{

                            JSONObject jsonObject = new JSONObject(response);
                            String success = jsonObject.getString("success");
                            if(success.equals("1")){
                                Toast.makeText(teste.this, "Enviado com sucesso", Toast.LENGTH_LONG).show();

                            }

                        }catch (JSONException e){

                            e.printStackTrace();
                            Toast.makeText(teste.this, "Erro" + e + toString(), Toast.LENGTH_SHORT).show();

                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(teste.this, "Erro 2" +error.toString(), Toast.LENGTH_SHORT).show();

                    }
                }){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("latitude", latitude);
                params.put("longitude", longitude);
                return params;
            }


        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }
}

Мне кажется, проблема в этом коде заключается в том, что я отправляю "долготу" и "широту" в виде строки. База данных ожидает двойной тип. Вот мой php:

<?php

    require_once 'connect.php';
    $result = array();

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){

        $longitude = $_POST['longitude'];
        $latitude = $_POST['latitude'];

        //$latitude = 1.260;
        //$longitude = 1.250;
        $sql = "INSERT INTO localizacao (lati, longi) VALUES('$latitude', '$longitude')";

            if(mysqli_query($conn2, $sql)){
                $result["success"] = "1";
                $result["message"] = "success";     
        }else{
                $result["success"] = "0";
                $result["message"] = "error 1";
        }
        mysqli_close($conn2);
    }else{  
            $result["success"] = "0";
            $result["message"] = "error 2"; 
    }


    echo json_encode($result);  

?>
...