【发布时间】:2018-10-24 10:57:33
【问题描述】:
我想从 Android 应用程序发送 PHP 页面上的数据,并将 PHP 页面存储数据存储在 My SQL 中。
当我将数据发送到 PHP 并存储数据库时。 android 应用程序成功向 php 页面发送请求,并将数据存储在数据库中。但是在 Android 应用程序中输入的值不能存储在数据库中。
PHP 代码
$user_name = $_POST['user_name'];
$user_mail = $_POST['email'];
$user_pass = $_POST['password'];
$user_ip = getRealIpAddr();
$sql = "INSERT INTO users (username, email, password, ip_add, created_at) VALUES ('$user_name', '$user_mail', '$user_pass', '$user_ip',Now())";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
和 Android XML 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="User Name"
android:inputType="text"
android:layout_gravity="center"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="38dp" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:layout_gravity="center"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="94dp" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_gravity="center"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="160dp" />
<Button
android:id="@+id/signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:background="@color/colorPrimary"
android:textColor="#fff"
android:layout_gravity="center"
tools:layout_editor_absoluteX="60dp"
tools:layout_editor_absoluteY="217dp"
android:onClick="onsignup"/>
</LinearLayout>
这里是Java代码
username = (EditText)findViewById(R.id.username);
email = (EditText)findViewById(R.id.email);
password = (EditText)findViewById(R.id.password);
}
public void onsignup(View view){
String user = username.getText().toString();
String mail = email.getText().toString();
String pass = password.getText().toString();
String type = "signup";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type,user,mail,pass);
}
BackgroundWorker 类代码在这里
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params){
String type = params[0];
String signup_url = "http://192.168.0.104/android/signup.php";
if (type.equals("signup")) {
try {
String user_name = params[1];
String email = params[2];
String password = params[3];
URL url = new URL(signup_url);
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("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
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();
alertDialog.setTitle("Signup Status");
}
@Override
protected void onPostExecute(String user_name) {
alertDialog.setMessage(user_name);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
这是编码的所有文件我也有清单文件的权限 但我不知道问题到底出在哪里。 感谢您的帮助
【问题讨论】:
-
在你的代码中你在哪里存储数据?没有数据库,也没有共享首选项。
-
如果您看到最后一个代码,我会将数据发送到我的 php 页面链接,然后将 php 代码存储在数据库中
-
我看到完整的 xml 清单(对这篇文章没用)和不完整的 php(可能是最有用的)。你甚至知道 php 是否被调用?调试、异常处理、某种日志记录、apache 日志、php 日志??????什么?
-
@YvesLeBorg 是的,我有关于日志记录的想法,但 Android java 类 BackgroundWorker 中的主要问题是不发送放入 XML 文件的数据
-
好的...堆栈跟踪? http状态?编译错误?调试日志?日志猫?什么???
标签: php android mysql database sqlite