在Android应用程序上注册用户时拒绝来自服务器的响应

时间:2017-03-09 07:39:37

标签: java php mysql sql android-studio

我在Android应用程序上注册用户时出现问题我正在建立,我收到服务器的拒绝回复,这是我得到的错误在调试时:

    <br />
<b>Warning</b>:  mysqli_connect(): (HY000/2002): Connection refused in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  mysqli_prepare() expects parameter 1 to be mysqli, boolean given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>21</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_store_result() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>22</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_num_rows() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>23</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>24</b><br />
<br />
<b>Warning</b>:  mysqli_prepare() expects parameter 1 to be mysqli, boolean given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>12</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>13</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>14</b><br />
<br />
<b>Warning</b>:  mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in <b>/storage/h4/905/1013905/public_html/register.php</b> on line <b>15</b><br />
{"success":true}

这是我的php文件:

<?php

    $connect = mysqli_connect("localhost:3306", "id1013905_user", "MY PASSWORD", "id1013905_trhtkv");

    $name = $_POST["name"];
    $age = $_POST["age"];
    $username = $_POST["username"];
    $password = $_POST["password"];
     function registerUser() {
        global $connect, $name, $age, $username, $password;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);
        $statement = mysqli_prepare($connect, "INSERT INTO user (name, age, username, password) VALUES (?, ?, ?, ?)");
        mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $passwordHash);
        mysqli_stmt_execute($statement);
        mysqli_stmt_close($statement);     
    }
    function usernameAvailable() {
        global $connect, $username;
        $statement = mysqli_prepare($connect, "SELECT * FROM user WHERE username = ?"); 
        mysqli_stmt_bind_param($statement, "s", $username);
        mysqli_stmt_execute($statement);
        mysqli_stmt_store_result($statement);
        $count = mysqli_stmt_num_rows($statement);
        mysqli_stmt_close($statement); 
        if ($count < 1){
            return true; 
        }else {
            return false; 
        }
    }
    $response = array();
    $response["success"] = false;  
    if (usernameAvailable()){
        registerUser();
        $response["success"] = true;  
    }

    echo json_encode($response);
    ?>

注册类我使用:

package com.ben.owner.say_something;

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

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

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

import java.util.Queue;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        final EditText etAge = (EditText)findViewById(R.id.etAge);
        final EditText etName = (EditText)findViewById(R.id.etName);
        final EditText etUsername = (EditText)findViewById(R.id.etUsername);
        final EditText etPassword = (EditText)findViewById(R.id.etPassword);
        final Button bRegister = (Button) findViewById(R.id.bRegister);

        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = etName.getText().toString();
                final String username = etUsername.getText().toString();
                final String password = etPassword.getText().toString();
                final int age = Integer.parseInt(etAge.getText().toString());

                Response.Listener<String>responseListener = new Response.Listener<String>(){

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");

                            if (success){
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }

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

                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(name, username, age, password,responseListener );
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}

注册请求:

package com.ben.owner.say_something;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by owner on 08/03/2017.
 */
public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "https://saysomething.000webhostapp.com/register.php";
    private Map<String, String>params;

    public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener){
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("username", username);
        params.put("password", password);
        params.put("age", age + "");
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

请帮助我理解我做错了什么,PHP不是我强有力的一面。

0 个答案:

没有答案