我使用000webhost创建了一个数据库,并将我的php文件上传到该服务器。我正在使用android studio创建用户注册页面。我单击以转到注册页面,键入所有信息,然后单击“提交”按钮,这使我回到登录页面,这应该做,但是该信息未发送到我的数据库。我没有收到错误消息,所以我不知道从哪里开始。这是我的Java类和php文件中的代码以及清单文件的片段。
注册请求java类
package amp.riot;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL ="https://upbeat.000webhostapp.com/register.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("username", username);
params.put("password", password);
params.put("age", age + "");
}
public Map<String, String> getParams(){
return params;
}
}
注册页面java类
public class Register extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etAge= (EditText) findViewById(R.id.etAge);
final EditText etName= (EditText) findViewById(R.id.etName);
final EditText etUsername= (EditText) findViewById(R.id.etUsername);
final EditText etPassword= (EditText) findViewById(R.id.etPassword);
final Button bRegister= (Button) findViewById(R.id.bRegister);
bRegister.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
final String name=etName.getText().toString();
final String username=etUsername.getText().toString();
final String password=etPassword.getText().toString();
final int age= Integer.parseInt(etAge.getText().toString());
final 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(Register.this, Login.class);
Register.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(Register.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(Register.this);
queue.add(registerRequest);
}
});
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="amp.riot">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
注册PHP文件
<?php
$con = mysqli_connect("localhost", "id202020_sierra", "05alop59", "id202020_plank");
$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);
?>
参考方案
尝试使用StringRequest
而不是使用Response.Listener<String>
StringRequest strreq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("id",id);
params.put("name",editText_name.getText().toString());
params.put("pass",editText_pass.getText().toString());
return params;
}
};
AppController.getInstance().addToRequestQueue(strreq);
有没有一种方法可以在一个mediaplayer对象中播放mp3文件? - java我可以使用Java android中的Mediaplayer类播放声音。我这样制作一个Mediaplayer对象。Mediaplayer mp=Mediaplayer.create(this,R.raw.mysong); 然后开始播放,但是如果我有一首以上的歌曲必须播放。我需要为每个人都做对象吗?我要说的是Mediaplayer类是否具有可以清除旧歌曲并放上…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
Visual Studio Code可以增量编译Java Web App项目吗? - java我是Visual Studio Code的新手。我正在尝试将其与使用Maven的现有Java Web应用程序项目一起使用。我已经下载了推荐的Java扩展,例如Java Extension Pack,Java Maven和Java Tomcat。在IntelliJ(我通常的IDE)中,我可以很快地从Java的少量编辑转到Tomcat中刷新的WAR。如果我编辑文…
Java-父类正在从子类中调用方法? - java抱歉,我还是编码的新手,可能还没有掌握所有术语。希望您仍然能理解我的问题。我想得到的输出是:"Cost for Parent is: 77.77" "Cost for Child is: 33.33" 但是,我得到这个:"Cost for Parent is: 33.33" "Cost f…
java.net.URI.create异常 - javajava.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…