我嘗試使用下面的代碼從Android應用程序發送經緯度服務器
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或「android.extends ....」
我是非常新的android和不知道問題是。任何幫助,將不勝感激!!!
首先檢查/調試,你得到緯度長或沒有,然後嘗試檢查數據傳遞到服務器的正常與否?之後檢查您的PHP代碼數據庫。 – AmeeJoshi
是的,我得到了正確的經度和緯度。他們得到顯示。數據傳遞到服務器時發生問題。 – John
我認爲你的問題不清楚有服務器或Android端代碼的問題? –