Here is my code
1.Java Code
class makePOST extends AsyncTask<String, Void, Double> {
int duration = Toast.LENGTH_SHORT;
@Override
protected void onPreExecute() {
Toast.makeText(update.this, "onPreExecute", Toast.LENGTH_LONG).show();
}
protected Double doInBackground(String... params) {
try {
url = new URL("http://student.96.lt/Android/update.php");
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
String postdat = "code" + "=" + URLEncoder.encode(mCode, "UTF-8") + "&" + "latitude" + "=" + URLEncoder.encode(mLatitudeText, "UTF-8") + "&" + "longitude" + "=" + URLEncoder.encode(mLongitudeText, "UTF-8");
//String postdat=mLatitudeText;
conn.setFixedLengthStreamingMode(postdat.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream OS = conn.getOutputStream();
InputStream in = conn.getInputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
bufferedWriter.write(postdat);
bufferedWriter.flush();
// Get output
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("
");
}
in.close();
bufferedWriter.close();
// java.io.BufferedOutputStream out = new java.io.BufferedOutputStream(conn.getOutputStream());
// PrintStream pstream = new PrintStream(out);
// pstream.print(postdat);
// pstream.close();
} catch (java.net.MalformedURLException ex) {
//Toast.makeText(this, ex.toString(), duration ).show();
} catch (java.io.IOException ex) {
//Toast.makeText(this, ex.toString(), duration).show();
}
return null;
}
};
2. PHP Code(update.php)
<?php
include 'connect.php';
$m_lat=$_POST['latitude'];
$m_lng=$_POST['longitude'];
$m_code=$_POST['code'];
$query="update `location` set `longitude`='$m_lng',`latitude`='$m_lat' where `code`='$m_code'";
mysqli_query($connect,$query);
mysqli_close($connect);
?>
3. AndroidManifest.xml File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.nikhil.gpslocation">
<permission
android:name="com.javapapers.currentlocationinmap.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.javapapers.currentlocationinmap.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-feature
android:name="android.hardware.location.gps"
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MyKey" />
<activity
android:name=".update"
android:label="@string/title_activity_update"
android:exported="true"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And I also want to update longitude and latitude every 10 seconds. This is my first app. I'm at beginning level of Android development.
Now my PHP code can be seen at: student.96.lt/Android/test.php