通过我的Android应用程序插入我的数据库时,插入已完成,但字段仍为空。 但是,如果我是通过谷歌浏览器进行的,例如,通过输入脚本的URL和一些参数,如下所示,它可以正常工作。
http://mywebsite.com/register.inc.php?username=paul&password=1234
我有什么问题吗?我觉得这是一个小错误,很可能是在我的java类中,因为脚本通过html工作,但不是在我的Android应用程序中。
一如既往地感谢您的时间和帮助!
这是我的php脚本:
<?php
include 'config.inc.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = $_GET['username'];
$password = $_GET['password'];
$sql = "INSERT INTO tbl_login
VALUES ('$username', '$password')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
这是我用来启动脚本的java类:
private class AsyncInsert extends AsyncTask<String, String, String>
{
ProgressDialog pdLoading = new ProgressDialog(CreateAnAccount.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://barldroid.com/register.inc.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("username", params[0])
.appendQueryParameter("password", params[1]);
String query = builder.build().getEncodedQuery();
// Open connection for sending data
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "exception";
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
Log.d("Charles", "connection réussie");
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return(result.toString());
}else{
Log.d("Charles", "connection échouée");
return("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return "exception";
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
if(result.equalsIgnoreCase("New record created successfully"))
{
/* Here launching another activity when login successful. If you persist login state
use sharedPreferences of Android. and logout button to clear sharedPreferences.
*/
Log.d("Charles", "logged in");
//addUserToDatabase(etEmail.getText().toString(), etPassword.getText().toString());
Toast.makeText(CreateAnAccount.this, "Account created. \n Welcome to Botnet.", Toast.LENGTH_LONG).show();
returnToLogin();
}else if (result.equalsIgnoreCase("false")){
// If username and password does not match display a error message
Toast.makeText(CreateAnAccount.this, "Invalid email or password", Toast.LENGTH_LONG).show();
} else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {
Toast.makeText(CreateAnAccount.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();
}
}
}