这是我第一次问问题,因为我真的遇到了一个很大的问题,我正在开发一个可以与远程服务器通信的android登录/注册页面,我遵循了一些教程,但我仍然无法得到理想的结果,即使我遵循了很多教程,我希望有人能帮助我。
以下是我的代码,我首先在localhost中创建了一个mysql数据库,并编写了以下php代码来连接android应用程序和数据库:
<?php
$con = mysqli_connect("localhost", "myaccount", "mypassword", "mytest");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, username, age, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
然后我创建了一个android应用程序,它使用volley框架来处理数据库,这里是“注册”按钮的代码,当我填写信息并点击它时,我希望它可以发送数据tp mysql,
RegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = etName.getText().toString();
final String username = etUsername.getText().toString();
final int age = Integer.parseInt(etAge.getText().toString());
final String password = etPassword.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
我还列出了如下的RegisterRequest
函数代码:
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://my computer ipv4/Register11.php";
private Map<String, String> params;
public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener) {
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("name", name);
params.put("age", age + "");
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
我个人认为我已经完成了所有需要的信息,但是当我测试我的andriod应用程序时,我无法将数据发送到数据库,我检查了一整天的代码仍然无法解决它,在这里我发布了我的register
页面,
register page,register page after finish information
当我点击“注册”按钮时,应用程序会给出一个警告
No Network Security Config specified, using platform default
我知道这不是错误,但我的应用程序在这里停止了,我的意思是,当我点击按钮时,应用程序没有响应,只显示上面的警告,而且,信息没有发送到数据库,我已经尝试了很长时间,但仍然得到错误,我希望有人能帮助我。我将非常感谢!
发布于 2018-08-31 06:12:33
从Google的网络安全配置文档中,您可以通过添加以下内容来修复此问题-
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
...
<application
...
android:usesClearTextTraffic="true"
...>
...
</application>
</manifest>
添加到AndroidManifest.xml文件中
有关更多信息,您可以查看谷歌文档,因此网络安全配置在这里- "https://developer.android.com/training/articles/security-config“
发布于 2019-05-02 18:22:15
Manifest文件中的更改帮助了我
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
...
<application
...
android:usesClearTextTraffic="true"
android:icon="@mipmap/ic_launcher"
...>
...
</application>
</manifest>
https://stackoverflow.com/questions/51802525
复制相似问题