在Android应用程序中没有正确的服务器响应

时间:2016-06-13 00:28:08

标签: java php android server-response

AsyncTask主要方法:

    public class ConnectToServerAsyncTask extends AsyncTask<String, Void, String> {
    private Context context;
    public ConnectToServerAsyncTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Synchronizing...........");
    }

    @Override
    protected String doInBackground(String... params) {
        String order = params[0];
        if (order.equalsIgnoreCase("Register")) {
            String name = params[1];
            String username = params[2];
            String password = params[3];
            String link = SERVER_ADDRESS + "signUp.php";
            try {
                String dataToSend =  URLEncoder.encode("name","UTF-8")+"=" + URLEncoder.encode(name, "UTF-8") +"&"+
                              URLEncoder.encode("username","UTF-8")+"="+ URLEncoder.encode(username, "UTF-8")+"&"+
                              URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");

                URL url = new URL(link);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("POST");
                con.setDoOutput(true);
                con.setDoInput(true);
                OutputStream outputStream = con.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                bufferedWriter.write(dataToSend);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputStream = con.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String result = "";
                while(bufferedReader.readLine()!=null) {
                     result+= bufferedReader.readLine();
                }
                bufferedReader.close();
                inputStream.close();
                con.disconnect();
                return result;
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        if (result!=null) {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
        else {
        }
    }
}

PHP:

$con = mysqli_connect("**********.com","**********name","*********","*******_PARKLOG");

if (mysqli_connect_errno($con)){
   echo "Error Connect to database";
}

$name = $_POST["name"];
$username = $_POST["username"];
$password = $_POST["password"];

$check = mysqli_prepare($con,"select * from account where username = ?");
mysqli_stmt_bind_param($check,"s",$username);
mysqli_stmt_execute($check);
mysqli_stmt_store_result($check);
$row_count = mysqli_stmt_num_rows($check);
if($row_count == 1){
    echo "User already exist, please change a username";
}
else{
    $statement  = mysqli_prepare($con,"INSERT INTO account (name,username,password) VALUES (?,?,?)");
    mysqli_stmt_bind_param($statement,"sss",$name,$username,$password);
    $result = mysqli_stmt_execute($statement);

    if($result == true) {
        echo "Sign up successful";
    }
    else{
        echo "Fail doing operation in database";
    }
}
//mysqli_stmt_close($statement);
mysqli_close($con);

它应该显示php消息,如“注册成功”或“用户已存在,请更改用户名”或任何其他类型的错误消息,但它只显示-1。这意味着有一些地方给我错误的服务器响应。哪里错了,我应该如何修复它。(此外,这个程序将处理数据插入数据库,但它会给出相同的消息-1)(android宣言文件是正确的。)

1 个答案:

答案 0 :(得分:0)

即使我不想在我的项目中使用额外的库,也可以通过使用排球来解决问题,无论如何,谢谢