我最近调整了我的android studio app,以1秒的间隔上传LatLon数据。在此之前,我使用按钮定期上传数据,这种方法工作正常,我可以在MySQL中看到数据。
我仍在使用相同的PHP脚本在MYSQL数据库中发布数据。 我的logcat显示它尝试发送数据,但gry.php文件不存在。
Android Studio功能:
public void uploadInfo(View view){
BackgroundOp backgroundOp = new BackgroundOp();
backgroundOp.execute(latString, lonString);
}
class BackgroundOp extends AsyncTask<String, Void, String> {
String myUrl;
@Override
protected void onPreExecute() {
myUrl = "[web address]/qry.php";
}
@Override
protected String doInBackground(String... data) {
String latString, lonString;
latString = data [0];
lonString = data [1];
try {
URL url = new URL(myUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String myDataStream = URLEncoder.encode("Latitude", "UTF-8") + "=" + URLEncoder.encode(latString, "UTF-8") + "&" +
URLEncoder.encode("Longitude", "UTF-8") + "=" + URLEncoder.encode(lonString, "UTF-8");
bufferedWriter.write(myDataStream);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();
httpURLConnection.disconnect();
return "Upload Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
qry.php:
<?php
require "params.php";
$Latitude = $_POST["Latitude"];
$Longitude = $_POST["Longitude"];
$Time = $_POST["Time"];
$sql_query = "insert into loc_info values('$Latitude', '$Longitude', '$Time');";
if (mysqli_query($connect, $sql_query)){
echo "<br>Data writing successful";
} else {
echo "<br>Data writing failed".mysqli_error($connect);
}
?>
logcat的:
04-13 12:58:42.770 29918-29918/? E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
04-13 12:58:42.770 29918-29918/? E/GMPM: Scheduler not set. Not logging error/warn.
04-13 12:58:42.779 29918-29971/? E/GMPM: Uploading is not possible. App measurement disabled
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err:
java.io.FileNotFoundException: [web address]/qry.php
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at com.cityuni.sophie.locationapp.MainActivity$BackgroundOp.doInBackground(MainActivity.java:198)
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at com.cityuni.sophie.locationapp.MainActivity$BackgroundOp.doInBackground(MainActivity.java:170)
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-13 12:59:31.499 29918-30731/com.cityuni.sophie.locationapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-13 12:59:31.502 29918-30731/com.cityuni.sophie.locationapp W/System.err: at java.lang.Thread.run(Thread.java:818)