Java Android-未将值插入数据库
我想要的是通过android应用程序将名称、用户名和密码传递到mysql(phpmyadmin)数据库。当我点击注册按钮时,它向我展示了注册成功的祝酒词。但当我签入数据库时,并没有插入任何值,数据库仍然是空的。我不打算分享我的php脚本,因为当我粘贴所有php脚本的url时,它们都很好。它很好用。但我会按要求分享 BackgroundTask.javaJava Android-未将值插入数据库,java,android,mysql,xml,Java,Android,Mysql,Xml,我想要的是通过android应用程序将名称、用户名和密码传递到mysql(phpmyadmin)数据库。当我点击注册按钮时,它向我展示了注册成功的祝酒词。但当我签入数据库时,并没有插入任何值,数据库仍然是空的。我不打算分享我的php脚本,因为当我粘贴所有php脚本的url时,它们都很好。它很好用。但我会按要求分享 BackgroundTask.java package com.example.abdul.mysqldbdemo; import android.app.AlertDialog;
package com.example.abdul.mysqldbdemo;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundTask extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx =ctx;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information....");
}
@Override
protected String doInBackground(String... params) {
String reg_url="http://zamin.byethost14.com/register.php";
String login_url="http://zamin.byethost14.com/login.php";
String method = params[0];
if (method.equals("register")) {
String name = params[1];
String user_name = params[2];
String user_pass = params[3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(120000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("user", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+"&"+
URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8")+"&"+
URLEncoder.encode("user_pass", "UTF-8")+"="+URLEncoder.encode(user_pass, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
httpURLConnection.connect();
httpURLConnection.disconnect();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("login"))
{
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(120000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Registration Success..."))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else
{
alertDialog.setMessage(result);
alertDialog.show();
}
}
}
activity_register.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffff7b57"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.abdul.mysqldbdemo.Register">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="40dp"
android:layout_gravity="center_horizontal"
android:textColor="#000"
android:text="Register Form"
/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/name"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="User Name"
android:id="@+id/new_user_name"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="Password"
android:id="@+id/new_user_pass"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:inputType="textPassword"/>
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="Register"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:onClick="userReg"/>
</LinearLayout>
register.php
<?php
require"init.php";
$name=$_POST["user"];
$user_name=$_POST["user_name"];
$user_pass=$_POST["user_pass"];
$sql_query="insert into user_info values('$name','$user_name','$user_pass');";
if(mysqli_query($con,$sql_query))
{
echo"<h3>Data Insertion Success...</h3>";
}
else
{
echo "Data insertion error".mysqli_error($con);
}
?>
试试这个方法,希望有帮助。
@Override
protected String doInBackground(String... params) {
String reg_url="http://zamin.byethost14.com/register.php";
String login_url="http://zamin.byethost14.com/login.php";
String method = params[0];
if (method.equals("register")) {
String name = params[1];
String user_name = params[2];
String user_pass = params[3];
try {
URL url = new URL(reg_url);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
String data = URLEncoder.encode("user", "UTF-8")+"="+name,+"&"+
URLEncoder.encode("user_name", "UTF-8")+"="+user_name+"&"+
URLEncoder.encode("user_pass", "UTF-8")+"="+user_pass;
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("login"))
{
String login_name = params[1];
String login_pass = params[2];
BufferedReader bufferedReader = null
try {
URL url = new URL(login_url);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
String data = URLEncoder.encode("login_name","UTF-8")+"="login_name+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+login_pass;
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
找到的解决方案:对来自应用程序UI的UTF字符进行编码
username = URLEncoder.encode(username, "UTF-8");
你做出承诺了吗?你说了什么?我没有在哪个文件中找到像$mysqli->commit()这样的东西?我有3个php文件。init.php(关于数据库连接)、register.php(我在上面分享过)和login.php.register.php,如果您用文字解释是什么让它成为答案,您的答案会好很多倍。@Akhil Ashok此代码与我的项目无关。
username = URLEncoder.encode(username, "UTF-8");