我想要的是通过Android应用程序将名称,用户名,密码传递到mysql(phpmyadmin)数据库。当我点击注册按钮时,它向我显示注册成功的祝酒词。但是当我检查数据库时没有插入任何值,数据库仍然是空的。我不打算分享我的PHP脚本,因为当我粘贴所有PHP脚本的url时它们都很好。它工作正常。但我会按要求分享。
BackgroundTask.java
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();
}
}
}
Register.java
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class Register extends Activity {
EditText ET_NAME,ET_USER_NAME,ET_USER_PASS;
String name,user_name,user_pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ET_NAME=(EditText)findViewById(R.id.name);
ET_USER_NAME=(EditText)findViewById(R.id.new_user_name);
ET_USER_PASS=(EditText)findViewById(R.id.new_user_pass);
}
public void userReg(View view)
{
name= ET_NAME.getText().toString();
user_name= ET_USER_NAME.getText().toString();
user_pass= ET_USER_PASS.getText().toString();
String method="register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method,name,user_name,user_pass);
finish();
}
}
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);
}
?>
答案 0 :(得分:0)
尝试这种方法希望有所帮助。
@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;
}
答案 1 :(得分:0)
找到解决方案:编码来自应用UI的UTF字符。
username = URLEncoder.encode(username, "UTF-8");