dongyuduan1890 2016-11-09 19:40
浏览 88
已采纳

为什么我无法连接到我的数据库

I've set up an apache2 ubuntu server on AWS, I create a mySQL db in said server, now I'm trying to update the database from my android application but its not connecting.

This is the PHP file in the server side which should connect to the db.

<?php
 define('HOST','ec2-54-171-67-69.eu-west-1.compute.amazonaws.com');
 define('USER','root');
 define('PASS','password');
 define('DB','ugproject');

 $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
 ?>

This is the Java method.

private static final String REGISTER_URL = "ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php";
 private void registerUser(){
        final String username = editTextUsername.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();
        final String email = editTextEmail.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);  
                params.put(KEY_PASSWORD,password);  
                params.put(KEY_EMAIL, email);  
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

This is the error I get:

E/Volley: [126] BasicNetwork.performRequest: Unexpected response code 412 for http://ubuntu@ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php       

I've tried to be as clear as possible feel free to ask for any more info, thanks.

  • 写回答

3条回答 默认 最新

  • dongsui0929 2016-11-11 19:33
    关注

    Error code 412 means

    Precondition Failed

    The precondition given in one or more of the request-header fields evaluated to false when it was tested on the server. This response code allows the client to place preconditions on the current resource metainformation (header field data) and thus prevent the requested method from being applied to a resource other than the one intended.

    This is likely cause by the fact that the URL that you provided

    http://ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php

    Seems to be running a webserver, but is not properly interpreting the PHP script - it displays to me the raw text of the PHP file.

    <?php
    
     if($_SERVER['REQUEST_METHOD']=='POST'){
     $username = $_POST['username'];
     $email = $_POST['email'];
     $password = $_POST['password'];
    
     require_once('dbConnect.php');
    
     $sql = "INSERT INTO users (username,password,email) VALUES ('$username','$email','$password')";
    
    
     if(mysqli_query($con,$sql)){
     echo "Successfully Registered";
     }else{
     echo "Could not register";
    
     }
     }else{
    echo 'error';
    }
    ?>
    

    Perhaps you do not have the php plugin installed, or is is not properly configured.

    This is an issue on the server side, not on the app side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥50 成都蓉城足球俱乐部小程序抢票