Android通过PHP将数据插入MySQL数据库

时间:2016-05-24 10:55:52

标签: php android mysql

我的注册PHP程序

    <?php
    require "conn.php";
    $name =$_POST["name"];
    $email =$_POST["email"];
    $password =$_POST["password"];
    $sql_query = "insert into register (uname,email,upassword) values('$name','$email','$password');";
    if(mysqli_query($conn,$sql_query))
    {
    //echo "data inserted";
    }
    else
    {
    //echo "error";
    }
    ?>

RegistractionActivity布局

    package com.example.aclientz.aclientzcds;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
        EditText et_username,et_useremail,et_userpassword;
        Button register;
        String name,email,password;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            et_username=(EditText) findViewById(R.id.name);
            et_useremail=(EditText) findViewById(R.id.email);
            et_userpassword=(EditText) findViewById(R.id.password);
            register=(Button) findViewById(R.id.register);
            register.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            name = et_useremail.getText().toString();
            email = et_useremail.getText().toString();
            password = et_userpassword.getText().toString();
            String type="register";
            BackgroundProcess backgroundProcess = new BackgroundProcess(this);
            backgroundProcess.execute(type, name, email, password);
            finish();
        }
    }

我的异步后台进程页面代码

    package com.example.aclientz.aclientzcds;

    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;

    /**
     * Created by ProgrammingKnowledge on 1/5/2016.
     */
    public class BackgroundProcess extends AsyncTask<String,Void,String> {
        Context context;
        AlertDialog alertDialog;
        BackgroundProcess (Context ctx) {
            context = ctx;
        }
        @Override
        protected void onPreExecute() {
            alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("Login Status");
            }
        @Override
        protected String doInBackground(String... params) {
            String type = params[0];
            String login_url = "http://192.168.1.2/login.php";
            String register_url="http://192.168.1.2/reg.php";
            if (type.equals("register")) {
                String name = params[1];
                String email = params[2];
                String password = params[3];
                try {
                    URL url = new URL(register_url);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    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("name", "UTF-8") + "=" + URLEncoder.encode(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(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(type.equals("login")) {
                try {
                    String user_email = params[1];
                    String user_password = params[2];
                    URL url = new URL(login_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_email","UTF-8")+"="+URLEncoder.encode(user_email,"UTF-8")+"&"
                            +URLEncoder.encode("user_password","UTF-8")+"="+URLEncoder.encode(user_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 onPostExecute(String result) {
            if(result.equals("Registration Success..."))
            {
                Toast.makeText(context, result, Toast.LENGTH_LONG).show();
            }
            else
            {
                alertDialog.setMessage(result);
                alertDialog.show();
            }
           // Toast.makeText(context, result, Toast.LENGTH_LONG).show();

        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

我的Android显示以下错误,我的数据未插入  数据库

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aclientz.aclientzcds, PID: 29591
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.aclientz.aclientzcds.BackgroundProcess.onPostExecute(BackgroundProcess.java:110)
at  com.example.aclientz.aclientzcds.BackgroundProcess.onPostExecute(BackgroundProcess.java:23)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5576)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)

1 个答案:

答案 0 :(得分:0)

使用单引号:

 $name =$_POST['name'];
 $email =$_POST['email'];
 $password =$_POST['password'];

而不是双引号:

$name =$_POST["name"];
$email =$_POST["email"];
$password =$_POST["password"];
相关问题