赞
踩
我想在我的android应用程序中插入新数据,数据将被发送到MYSQL工作台数据库。但是当我尝试编辑新数据并单击“保存”按钮时,数据将不会进入数据库。当我尝试将其打印出来时,它得到了输入结果。我使用PHP文件让应用程序连接到数据库。设置方法有什么我想念的吗?
我的代码和Logcat没有错误。当我点击保存按钮时,我应该排队:
03-28 02:58:07.697 4985-5433 / advprog.mmu.ac.uk.projectapp I / System.out:测试:customerName = test&carName = test&appointmentDate = tte&email = testes&issueDescribe = tstes&timeForJob = testes
03-28 02:58:13.597 4985-5433 / advprog.mmu.ac.uk.projectapp W / zygote:验证void com.android.tools.profiler.support.network.HttpTracker $ InputStreamTracker。(java.io.InputStream ,com.android.tools.profiler.support.network.HttpTracker $ Connection)耗时143.013ms
03-28 02:58:15.090 4985-5031 / advprog.mmu.ac.uk.projectapp D / EGL_emulation:eglMakeCurrent:0x9e4afd00:ver 3 0(tinfo 0x9d53ea40)
03-28 02:58:15.285 4985-5031 / advprog.mmu.ac.uk.projectapp D / EGL_emulation:eglMakeCurrent:0x9e4afd00:ver 3 0(tinfo 0x9d53ea40)
03-28 02:58:16.752 4985-4993 / advprog.mmu.ac.uk.projectapp I / zygote:完整代码缓存集合,代码= 244KB,数据= 163KB代码缓存收集后,代码= 244KB,数据= 133KB
它从EditText获取值,但只是没有传递给数据库。
这是应用程序中的Java代码:
public class BackgroundWorker extends AsyncTask implements advprog.mmu.ac.uk.projectapp.BackgroundWorker {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
protected String doInBackground(String... params) {
String newUrl = "http://10.0.2.2:8080/projectWeb/insert.php";
String customerName = params[0];
String carName = params[1];
String appointmentDate = params[2];
String email = params[3];
String issueDescribe = params[4];
String timeForJob = params[5];
try {
URL url = new URL(newUrl);
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 postData = URLEncoder.encode("customerName", "UTF-8") + "=" + URLEncoder.encode(customerName, "UTF-8") +"&"
+ URLEncoder.encode("carName", "UTF-8") + "=" + URLEncoder.encode(carName, "UTF-8") +"&"
+ URLEncoder.encode("appointmentDate", "UTF-8") + "=" + URLEncoder.encode(appointmentDate, "UTF-8") +"&"
+ URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") +"&"
+ URLEncoder.encode("issueDescribe", "UTF-8") + "=" + URLEncoder.encode(issueDescribe, "UTF-8") +"&"
+ URLEncoder.encode("timeForJob", "UTF-8") + "=" + URLEncoder.encode(timeForJob, "UTF-8");
System.out.println("Test:" + postData);
bufferedWriter.write(postData);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
PHP文件:
php
$servername = "mudfoot.doc.stu.mmu.ac.uk:6306";
$username = "username";
$password = "password";
$dbname = "pangh";
$dbconn = mysqli_connect($servername, $username, $password, $dbname);
$cuName = $_POST["customerName"];
$caName = $_POST["carName"];
$appDate = $_POST["appointmentDate"];
$ema = $_POST["email"];
$isDescribe = $_POST["issueDescribe"];
$tiJob = $_POST["timeForJob"];
$sql = "INSERT INTO projectData(customerName, carName, appointmentDate, email, issueDescribe)
VALUES ('$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob')";
if($dbconn->query($sql) === TRUE) {
echo "Insert Successful";
} else {
echo "Error: " .$sql . "
" .$dbconn->error;
}
mysqli_close($dbconn);
?>
这是我的数据库表的截图:qazxsw poi
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。