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.
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.
Try this might work .
Separate the DB name
$con = mysql_connect (HOST, USER, PASS) OR die ('could not connect to MySQL.');
mysql_select_db(DB);
Based on this link in one of your comments (here), it doesn't look like you have PHP installed on the server since it's printing out the code.
Easiest way to install it is using sudo apt-get install lamp-server^
command on Ubuntu.
If you just need PHP, use sudo apt-get install php7.0
. Once you install it, then use sudo service apache2 restart
to restart your apache server to apply the PHP installation.
To test if PHP is up and running, write this code into a file, and you should get a table full of data about the PHP installation when you go to it in your browser:
<?php
phpinfo();
?>