从Android应用程序向服务器发送经度和纬度

时间:2017-04-01 12:54:35

标签: php android google-maps

我尝试使用以下代码发送纬度和经度

package com.example.john.bustrackgps;

import android.Manifest;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements LocationListener {

    LocationManager locationManager;
    String mprovider;

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

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();

        mprovider = locationManager.getBestProvider(criteria, false);

        if (mprovider != null && !mprovider.equals("")) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            Location location = locationManager.getLastKnownLocation(mprovider);
            locationManager.requestLocationUpdates(mprovider, 15000, 1, this);

            if (location != null)
                onLocationChanged(location);
            else
                Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show();
        }
    }

    public void onLocationChanged(Location location) {
        TextView longitude = (TextView) findViewById(R.id.textView);
        TextView latitude = (TextView) findViewById(R.id.textView1);

        longitude.setText("Current Longitude: " + location.getLongitude());
        latitude.setText("Current Latitude: " + location.getLatitude());

        Response.Listener<String> responseListener = new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    boolean success = jsonResponse.getBoolean("success");
                    if (success) {
                        Intent intent = new Intent(MainActivity.this, ServerUpload.class);
                        MainActivity.this.startActivity(intent);
                    } else {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setMessage("Upload failed")
                                .setNegativeButton("Retry", null)
                                .create()
                                .show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };


        ServerUpload registerRequest = new ServerUpload(latitude,longitude,responseListener);
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        queue.add(registerRequest);

    }

    public void onStatusChanged(String s,int i,Bundle bundle)
    {}

    public void onProviderEnabled(String s)
    {}

    public void onProviderDisabled(String s)
    {}
}

这是我的ServerUpload类

package com.example.john.bustrackgps;

import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;


public class ServerUpload extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://192.168.169.2/serverfiles/bts.php";
    private Map<String, String> params;

    public ServerUpload(TextView latitude, TextView longitude, Response.Listener<String> listener){

        super(Method.POST, REGISTER_REQUEST_URL, listener, null);

        final String latitude1 =latitude.toString();
        final String longitude1 =longitude.toString();
        params = new HashMap<>();
        params.put("latitude", latitude1);
        params.put("longitude", longitude1);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

这是我的bts.php代码

<?php

$con = mysqli_connect("127.0.0.1", "root", "", "bustrack");


$latitude = $_POST["latitude"];

$longitude = $_POST["longitude"];

$statement = mysqli_prepare($con, "INSERT INTO updates (latitude, longitude) VALUES (?, ?)");

mysqli_stmt_bind_param($statement, "ss", $latitude, $longitude);

mysqli_stmt_execute($statement);


$response = array();

$response["success"] = true;  


echo json_encode($response);

问题是数据没有输入数据库。它是0,0或者#34; android.extends ....&#34;

我对android很新,不确定问题是什么。任何帮助将不胜感激!!!

1 个答案:

答案 0 :(得分:0)

ServerUpload方法中修复此错误。

 final String latitude1 =latitude.getText().toString();
final String longitude1 =longitude.getText().toString();

试试这个

 public ServerUpload(TextView latitude, TextView longitude, Response.Listener<String> listener){

    super(Method.POST, REGISTER_REQUEST_URL, listener, null);

    final String latitude1 =latitude.getText().toString();
    final String longitude1 =longitude.getText().toString();
    params = new HashMap<>();
    params.put("latitude", latitude1);
    params.put("longitude", longitude1);
}