【发布时间】:2016-11-09 19:40:52
【问题描述】:
我已经在 AWS 上设置了一个 apache2 ubuntu 服务器,我在所述服务器中创建了一个 mySQL 数据库,现在我正在尝试从我的 android 应用程序更新数据库,但它没有连接。
这是服务器端应该连接到数据库的 PHP 文件。
<?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');
?>
这是 Java 方法。
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);
}
这是我得到的错误:
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
我已尽量说明清楚,请随时询问更多信息,谢谢。
【问题讨论】:
-
你检查HTTP 412 code了吗?
-
它说它与请求标头有关,但我不确定是什么。这也是连接到数据库的 php 文件的正确格式吗?
-
当我通过 PC 上的浏览器访问
http://ubuntu@ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/volleyRegister.php时,浏览器说You are about to log in to the site “ec2-54-171-67-69.eu-west-1.compute.amazonaws.com” with the username “ubuntu”, but the website does not require authentication. This may be an attempt to trick you.- 可能是这个原因吗?错误的 URL 字符串? -
你是对的,这是我试图改变的错误 URL,我在正确的 URL 上得到了同样的错误:ec2-54-171-67-69.eu-west-1.compute.amazonaws.com/…
标签: php android mysql android-volley