我想将这些数据从Android应用程序保存到服务器中的mysql数据库。我用PHP编写代码..当我只运行php文件数据将保存到数据库..但它运行形式Android应用程序。数据不保存到DB。
在Android中保存代码
public void loadSave() {
String mydataurl = "http://xxxxxxxxxx/saveData.php";
//String type = params[0];
try {
String longi = "aaa"; // Double.toString(location.getLongitude());
String lati = "aare";//Double.toString(location.getLatitude());
String user = "a";
String lisence = "b";
String date = "c";
URL url = new URL(mydataurl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("longi", "UTF-8") + "=" + URLEncoder.encode(longi, "UTF-8") + "&" +
URLEncoder.encode("lati", "UTF-8") + "=" + URLEncoder.encode(lati, "UTF-8") + "&" +
URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8") + "&" +
URLEncoder.encode("lisence", "UTF-8") + "=" + URLEncoder.encode(lisence, "UTF-8") + "&" +
URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date, "UTF-8");
bufferedWriter.write(mydataurl);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
} catch (MalformedInputException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
php代码
require "DB_Connection.php";
$longi =$_POST["longi"];
$lati = $_POST["lati"];
$user = $_POST["user"];
$lisence =$_POST["lisence"];
$date = $_POST["date"];
$query1="INSERT INTO mapdata (longi,lati,user,lisence,date)
values ('".$longi."','".$lati."','".$user."','".$lisence."', '".$date."' )";
mysql_query($query1) or die ('Error :' . mysql_error());
调用loadSave()
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(final Void... params) {
loadSave();
return null;
}
}.execute();