I have recently adjusted my android studio app to upload LatLon data at 1 second intervals. Previous to this, I was uploading the data using a button at less regular intervals, this method was working properly and I could see the data in MySQL.
I am still using the same PHP script to post the data in MYSQL database. My logcat shows that it attempts to send the data but the gry.php file does not exist.
Android Studio Function:
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)