解决方案:对于任何有类似问题的人来说,经过大量的游戏,我将重新创建项目作为最后的手段。所以我创建了一个新项目并将我的代码转移到那里。它成功了。我不知道为什么它有效但它确实
你好我这里有这个代码。当我使用应用程序时,它假设将数据发送到数据库,它做得很好,但它以某种方式停止。 我已检查并更改了我正在使用的IP地址
编辑:我曾尝试使用" Postman"添加数据到数据库。它实际上在数据库中添加了值。但应用程序的问题仍然存在。自从我上次测试以来,我没有换过一行。但不知何故,该应用甚至没有尝试发送数据
Android APP代码
MainActivity.java
package com.example.chara.crashcontrol;
import android.provider.Settings; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Base64; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast;
import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley;
import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map;
public class MainActivity extends AppCompatActivity {
Button btnShowLocation;
GPStracker gps;
EditText firstname,num,plate;
RequestQueue requestQueue;
CheckBox scene,traped,ambulance;
private String scene_is_checked = "no";
private String traped_is_checked = "no";
private String ambulance_is_checked ="no";
private String lat,lon;
String insertUrl = "http://MY_IP(censored)/storedata.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname = (EditText) findViewById(R.id.firstname);
num = (EditText) findViewById(R.id.num);
plate = (EditText) findViewById(R.id.plate);
scene = (CheckBox) findViewById(R.id.scene);
traped = (CheckBox) findViewById(R.id.traped);
ambulance = (CheckBox) findViewById(R.id.ambulance);
requestQueue = Volley.newRequestQueue(getApplicationContext());
btnShowLocation = (Button) findViewById(R.id.show_location);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gps = new GPStracker(MainActivity.this);
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
lat = Double.toString(latitude);
lon = Double.toString(longitude);
Toast.makeText(getApplicationContext(), "Your Location is =\nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
} else {
gps.showSettingsAlert();
}
if (ambulance.isChecked()){
ambulance_is_checked ="Yes";
}
if (scene.isChecked()) {
scene_is_checked = "Yes";
}
if (traped.isChecked()){
traped_is_checked = "Yes";
}
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
String e_fristname = firstname.getText().toString();
String efirstname = Base64.encodeToString(e_fristname.getBytes(), Base64.DEFAULT);
String e_nums = num.getText().toString();
String e_numb = Base64.encodeToString(e_nums.getBytes(), Base64.DEFAULT);
String e_plate = plate.getText().toString();
String eplate = Base64.encodeToString(e_plate.getBytes(), Base64.DEFAULT);
String e_scene = scene_is_checked.toString();
String escene = Base64.encodeToString(e_scene.getBytes(), Base64.DEFAULT);
String e_traped = traped_is_checked.toString();
String etraped = Base64.encodeToString(e_traped.getBytes(), Base64.DEFAULT);
String e_ambulance = ambulance_is_checked.toString();
String eambulance = Base64.encodeToString(e_ambulance.getBytes(), Base64.DEFAULT);
lat="43.232153";
lon="65.213422";
Map<String,String> parameters = new HashMap<String, String>();
parameters.put("firstname", efirstname.toString());
parameters.put("num", e_numb.toString());
parameters.put("plate",eplate.toString());
parameters.put("scene", escene.toString());
parameters.put("traped", etraped.toString());
parameters.put("ambulance",eambulance.toString());
parameters.put("lat",lat.toString());
parameters.put("lon", lon.toString());
btnShowLocation.setEnabled(false);
return parameters;
}
};
requestQueue.add(request);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(9000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
btnShowLocation.setEnabled(true);
}
});
}
}).start();
}
});
}
}
GetGPSlocation.Java(我没有这个,但它有效)
package com.example.chara.crashcontrol;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import java.util.concurrent.ExecutionException;
public class GPStracker extends Service implements LocationListener {
private final Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 9000;
protected LocationManager locationManager;
public GPStracker(Context context){
this.context = context;
getLocation();
}
public Location getLocation(){
try {
locationManager = (LocationManager)context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isGPSEnabled) {
} else {
this.canGetLocation = true;
if(isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if(isGPSEnabled) {
if(location == null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS(){
if(locationManager != null) {
locationManager.removeUpdates(GPStracker.this);
}
}
public double getLatitude() {
if(location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(location != null) {
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS SETTINGS");
alertDialog.setMessage("GPS is not enabled.Go to Settings menu and enable it?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
context.startActivity(intent);
}
}) ;
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
PHP代码
connection.php
<?php
define('hostname', 'myip(censored)');
define('user','root');
define('password','');
define('databaseName','crashcontrol');
$connect = mysqli_connect(hostname , user , password ,databaseName);
?>
storedata.php
<?php
if($_SERVER["REQUEST_METHOD"]=="POST") {
require 'connection.php';
createData();
}
function createData()
{
global $connect;
$firstname = $_POST["firstname"];
$num = $_POST["num"];
$plate = $_POST["plate"];
$scene = $_POST["scene"];
$ambulance = $_POST["ambulance"];
$traped = $_POST["traped"];
$lon = $_POST["lon"];
$lat = $_POST["lat"];
$query = "INSERT INTO report(firstname,num,plate,scene,ambulance,traped,lon,lat) values ('$firstname','$num','$plate','$scene','$ambulance','$traped','$lon','$lat');";
mysqli_query($connect,$query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>
来自appchie和mysql的错误代码
appachie
[Mon Jan 11 18:28:36.296359 2016] [ssl:warn] [pid 12848:tid 504] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Jan 11 18:28:36.407439 2016] [core:warn] [pid 12848:tid 504] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Mon Jan 11 18:28:36.633599 2016] [ssl:warn] [pid 12848:tid 504] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Jan 11 18:28:36.710653 2016] [mpm_winnt:notice] [pid 12848:tid 504] AH00455: Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.1 configured -- resuming normal operations
[Mon Jan 11 18:28:36.710653 2016] [mpm_winnt:notice] [pid 12848:tid 504] AH00456: Apache Lounge VC14 Server built: Dec 9 2015 10:17:39
[Mon Jan 11 18:28:36.710653 2016] [core:notice] [pid 12848:tid 504] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Mon Jan 11 18:28:36.715157 2016] [mpm_winnt:notice] [pid 12848:tid 504] AH00418: Parent: Created child process 10884
[Mon Jan 11 18:28:38.454892 2016] [ssl:warn] [pid 10884:tid 560] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Jan 11 18:28:38.771117 2016] [ssl:warn] [pid 10884:tid 560] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Jan 11 18:28:38.848671 2016] [mpm_winnt:notice] [pid 10884:tid 560] AH00354: Child: Starting 150 worker threads.
MySQL的
2016-01-11 18:28:37 17d4 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB's internal memory allocator.
2016-01-11 18:28:37 6100 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-01-11 18:28:37 6100 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-11 18:28:37 6100 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2016-01-11 18:28:37 6100 [Note] InnoDB: Memory barrier is not used
2016-01-11 18:28:37 6100 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-01-11 18:28:37 6100 [Note] InnoDB: Not using CPU crc32 instructions
2016-01-11 18:28:37 6100 [Note] InnoDB: Initializing buffer pool, size = 16.0M
2016-01-11 18:28:37 6100 [Note] InnoDB: Completed initialization of buffer pool
2016-01-11 18:28:37 6100 [Note] InnoDB: Highest supported file format is Barracuda.
2016-01-11 18:28:37 6100 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-11 18:28:37 6100 [Note] InnoDB: Waiting for purge to start
2016-01-11 18:28:38 6100 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.26-74.0 started; log sequence number 2035625
2016-01-11 18:28:38 7364 [Note] InnoDB: Dumping buffer pool(s) not yet started
2016-01-11 18:28:38 6100 [Note] Plugin 'FEEDBACK' is disabled.
2016-01-11 18:28:38 6100 [Note] Server socket created on IP: '0.0.0.0'.
2016-01-11 18:28:38 6100 [Note] c:\xampp\mysql\bin\mysqld.exe: ready for connections.
Version: '10.1.9-MariaDB' socket: '' port: 3306 mariadb.org binary distribution