当我想在App(Android,Php)中注册时没有任何反应

时间:2017-04-24 14:21:02

标签: php android sql

我尝试注册应用程序,但单击按钮时没有任何反应。我已经在浏览器中测试了Register Php文件并且它可以工作。 Wamp Server在线。该代码在我的Android设备上运行。

我的Register.java:

import android.app.DownloadManager;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.MySingleton;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Register extends AppCompatActivity {
    Button reg_bn;
    EditText Name, Email, Username, Password, ConPassword;
    String name, email, username, password, conpassword;
    AlertDialog.Builder builder;
    String reg_url="http://192.168.178.26//register.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        reg_bn = (Button)findViewById(R.id.button);
        Name = (EditText) findViewById(R.id.edRName);
        Email = (EditText) findViewById(R.id.edEmail);
        Username = (EditText) findViewById(R.id.TFRusername);
        Password = (EditText) findViewById(R.id.TFRpassword);
        ConPassword = (EditText) findViewById(R.id.editText3);
        builder = new AlertDialog.Builder(Register.this);
             reg_bn.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     name = Name.getText().toString();
                     email = Email.getText().toString();
                     username = Username.getText().toString();
                     password = Password.getText().toString();
                     conpassword = ConPassword.getText().toString();

                     if (name.equals("") || email.equals("") || username.equals("") || password.equals("") || conpassword.equals("")) {
                         builder.setTitle("Something went wrong");
                         builder.setMessage("Fill everything");
                         displayAlert("input_error");
                     } else {
                         if (!(password.equals(conpassword))) {
                             builder.setTitle("Something went wrong");
                             builder.setMessage("Passwords arent matching");
                             displayAlert("input_error");
                         } else {
                             StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url,
                                     new Response.Listener<String>() {
                                         @Override
                                         public void onResponse(String response) {
                                             try {
                                                 JSONArray jsonArray = new JSONArray(response);
                                                 JSONObject jsonObject = jsonArray.getJSONObject(0);
                                                 String code = jsonObject.getString("code");
                                                 String message = jsonObject.getString("message");
                                                 builder.setTitle("Server Response");
                                                 builder.setMessage(message);
                                                 displayAlert(code);

                                             } catch (JSONException e) {
                                                 e.printStackTrace();
                                             }

                                         }

                                     }, new Response.ErrorListener() {
                                 @Override
                                 public void onErrorResponse(VolleyError error) {

                                 }
                             }) {
                                 @Override
                                 protected Map<String, String> getParams() throws AuthFailureError {
                                     Map<String, String> params = new HashMap<String, String>();
                                     params.put("name", name);
                                     params.put("email", email);
                                     params.put("username", username);
                                     params.put("password", password);
                                     return params;
                                 }
                             };
                             MySingleton.getInstance(Register.this).addToRequestque(stringRequest);
                         }
                     }

                     }
                 });
             }


    public void displayAlert(final String code)
    {
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (code.equals("input error)")) {
                    Password.setText("");
                    ConPassword.setText("");
                } else if (code.equals("reg_success")) {
                    finish();

                } else if (code.equals("reg_failed")) {
                    Name.setText("");
                    Email.setText("");
                    Username.setText("");
                    Password.setText("");
                    ConPassword.setText("");

                }

            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}

register.php:

<?php

require "init.php";
$name = $_POST["name"];
$email = $_POST["email"];
$user_name = $_POST["user_name"];
$password = $_POST["password"];

$sql = "select * from user_info where email like '".$email."';";

$result = mysqli_query($con, $sql);
$response = array();

if(mysqli_num_rows($result)>0)
{
    $code = "reg_failed";
    $message = "User already exists";
    array_push($response, array("code"=>$code, "message"=>$message));
    print_r(json_encode($response));
}
else
{
    $sql = "insert into user_info values('".$name."','".$email."','".$user_name."','".$password."');";

    $result = mysqli_query($con, $sql);

    $code = "reg_success";
    $message = "User is now in the System !";
    array_push($response, array("code"=>$code, "message"=>$message));
    print_r(json_encode($response));

}

mysqli_close($con);


?>

0 个答案:

没有答案