Я хочу отправлять данные на страницу PHP из приложения Android, а страница 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;
}
и XMl-файл Android
<?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);
}
}
Этовсе файлы кодирования у меня также есть разрешение Интернет файла манифеста, но я не знаю, проблема именно где это.Спасибо за помощь