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条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口