App没有在数据库中插入任何内容

时间:2016-02-13 20:29:27

标签: php android android-studio mysqli

我无法找出以下内容有什么问题,这是我的php代码:

<?php
include_once 'dbconnect.php';
if($_SERVER['REQUEST_METHOD']=='POST'){

$number= $_POST['number'];
$name = $_POST['name'];
$city = $_POST['city'];
$street = $_POST['street'];
$building = $_POST['building'];
$email = $_POST['email'];

$password = $_POST['pass'];

if($number=='' || $name=='' || $city=='' || $street=='' || $building=='' || $email=='' || $password=='')
{
echo 'Please Fill All Fields';
}

else {
$query = "SELECT * from account WHERE number = '$number' ";
$check = mysqli_fetch_array(mysqli_query($mysqli, $query));

if(isset($check))
{
    echo 'Account already exists';
}
else {
    $query="INSERT INTO account (number,name,city,street,building,email,password) "
            . "VALUES('$number','$name','$city','$street','$building','$email','$password')";

    if(mysqli_query($mysqli,$query)){
        echo "Succesfully Registered.";
    }

 else {
 echo 'Something went wrong, Please try again later.';
 }
}
mysqli_close($mysqli);
}

}
 else {
echo 'Error';
 }
?>

我的注册android类:

public class register extends AppCompatActivity implements View.OnClickListener{

EditText rgNumber,rgPass,rgName,rgCity,rgBuilding,rgStr,rgEmail,rgPass2;
Button bRegister,bCancel;

private static final String RegisterURL="http://hssolutions.tk/ibd/includes/register_user.php";
@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.bRegister:
            String password= rgPass.getText().toString().trim();
            String password2 = rgPass2.getText().toString().trim();
            if(password.equals(password2)){
                registerUser();

            }

            else{
                showErrorMessage("Passwords don't match.");
            }

    }
}

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

    rgNumber = (EditText) findViewById(R.id.rgNumber);
    rgPass = (EditText) findViewById(R.id.rgPass);
    rgName = (EditText) findViewById(R.id.rgName);
    rgBuilding = (EditText) findViewById(R.id.rgBlding);
    rgCity = (EditText) findViewById(R.id.rgCity);
    rgStr= (EditText) findViewById(R.id.rgStr);
    rgEmail = (EditText) findViewById(R.id.rgEmail);
    rgPass2 = (EditText) findViewById(R.id.rgPass2);
    bRegister = (Button) findViewById(R.id.bRegister);
    bCancel = (Button) findViewById(R.id.bCancel);

    bRegister.setOnClickListener(this);

}

private void showErrorMessage(String error){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(register.this);
    dialogBuilder.setMessage(error);
    dialogBuilder.setPositiveButton("OK", null);
    dialogBuilder.show();
}


private void registerUser(){
    String number = rgNumber.getText().toString().trim();
    String name = rgName.getText().toString().trim();
    String city = rgCity.getText().toString().trim();
    String building = rgBuilding.getText().toString().trim();
    String street= rgStr.getText().toString().trim();
    String email= rgEmail.getText().toString().trim();
    String password= rgPass.getText().toString().trim();

    register(number,name,city,building,street,email,password);
}
private void register(String number, String name, String city, String building, String street, String email, String password){

    class RegisterUser extends AsyncTask<String,Void,String>
    {
        ProgressDialog loading;
        RegisterUserClass ruc=new RegisterUserClass();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(register.this,"Please Wait",null,true,true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(String... params) {

            HashMap<String,String> data=new HashMap<String,String>();
            data.put("number",params[0]);
            data.put("name",params[1]);
            data.put("city",params[2]);
            data.put("street",params[3]);
            data.put("building",params[4]);
            data.put("email",params[5]);
            data.put("pass",params[6]);

            String result = ruc.sendPostRequest(RegisterURL,data);
            return result;

        }
    }

    RegisterUser ru= new RegisterUser();
    ru.execute(number,name,city,street,building,email,password);
}
}

以及应该发送请求的注册类:

public class RegisterUserClass {

public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams){
    URL url;
    String response= "";

    try
    {
        url= new URL(requestURL);

        HttpURLConnection con= (HttpURLConnection)url.openConnection();
        con.setReadTimeout(15000);
        con.setConnectTimeout(15000);
        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);

        OutputStream os= con.getOutputStream();
        BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));

        writer.write(getPostDataString(postDataParams));
        writer.flush();
        writer.close();

        os.close();

        int responseCode = con.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK)
        {
            BufferedReader reader= new BufferedReader(new InputStreamReader(con.getInputStream()));
            response=reader.readLine();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return response;
}

private String getPostDataString(HashMap<String,String> params) throws UnsupportedEncodingException
{
    StringBuilder result= new StringBuilder();
    boolean first = true;
    for(Map.Entry<String,String> entry:params.entrySet()){
        if(first)
        {
            first=false;
        }
        else
        {
            result.append("&");
        }

        result.append(URLEncoder.encode(entry.getKey(),"UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(),"UTF-8"));
    }

    return result.toString();
}
}

应用程序没有显示错误,但是toast返回空,而数据库没有插入任何内容,我在逻辑上跟踪代码但仍无法找到错误。

1 个答案:

答案 0 :(得分:1)

在这里,假设唯一的一行可能有问题,

if(responseCode == HttpURLConnection.HTTP_OK)
    {
        BufferedReader reader= new BufferedReader(new InputStreamReader(con.getInputStream()));
        response=reader.readLine();
    }

你能改变这个:

if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
        } else {
            response = null;

        }