我正在使用php连接外部数据库和Android。我确认将在Android上输入的数据插入到mysql表中。但是,在JSONObject上出现错误。我遵循了本教程。我只是在这里将本地数据库转换为外部数据库。 -https://github.com/GaKaRi/gakari_android/tree/master/Proj_Manager_v3
如果您能帮助我,我将不胜感激。
RegisterActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText idText = (EditText)findViewById(R.id.idText);
final EditText passwordText = (EditText)findViewById(R.id.passwordText);
final EditText nameText = (EditText)findViewById(R.id.nameText);
final EditText emailText = (EditText)findViewById(R.id.emailText);
final EditText phoneText = (EditText)findViewById(R.id.phoneText);
Button regbtn = (Button)findViewById(R.id.registerbtn);
regbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userID = idText.getText().toString();
String userPassword = passwordText.getText().toString();
String userName = nameText.getText().toString();
String userEmail = emailText.getText().toString();
String userPhone = phoneText.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){
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("register success!!")
.setPositiveButton("ok", null)
.create()
.show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("register fail!!")
.setNegativeButton("ok", null)
.create()
.show();
}
}catch(JSONException e){
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(userID, userPassword, userEmail, userName, userPhone, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
RegisterRequest.java
final static private String URL = "http://10.0.2.2:8080/Register.php";
private Map<String, String> parameters;
public RegisterRequest(String userID, String userPassword, String userEmail, String userName, String userPhone, Response.Listener<String> listener){
super(Method.POST, URL, listener, null);
parameters = new HashMap<>();
parameters.put("userID", userID);
parameters.put("userPassword", userPassword);
parameters.put("userEmail", userEmail);
parameters.put("userName", userName);
parameters.put("userPhone", userPhone);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return parameters;
}
}
register.php
<?php
header('Content-Type: application/json');
$mysql_hostname = 'mirpasec.ddns.net';
$mysql_username = 'extern_user';
$mysql_password = 'extern_pass';
$mysql_database = 'extern_user';
$mysql_port = '50106';
$con = new mysqli($mysql_hostname, $mysql_username, $mysql_password, $mysql_database, $mysql_port);
if($con){
echo "MySQL connect success";
}else{
echo "MySQL connect fail";
}
$userID = $_POST["userID"];
$userPassword = $_POST["userPassword"];
$userEmail = $_POST["userEmail"];
$userName = $_POST["userName"];
$userPhone = $_POST["userPhone"];
$statement = mysqli_prepare($con, "INSERT INTO USER VALUES(?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssss", $userID, $userPassword, $userEmail, $userName, $userPhone);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
错误消息
04-30 15:16:40.651 6063-6063/com.example.ijangmin.stock W/System.err:org.json.JSONException: Value MySQL of type java.lang.String cannot be converted to JSONObject
04-30 15:16:40.652 6063-6063/com.example.ijangmin.stock W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
04-30 15:16:40.652 6063-6063/com.example.ijangmin.stock W/System.err: at org.json.JSONObject.<init>(JSONObject.java:163)
04-30 15:16:40.652 6063-6063/com.example.ijangmin.stock W/System.err: at org.json.JSONObject.<init>(JSONObject.java:176)
04-30 15:16:40.653 6063-6063/com.example.ijangmin.stock W/System.err: at com.example.ijangmin.stock.RegisterActivity$1$1.onResponse(RegisterActivity.java:58)
04-30 15:16:40.653 6063-6063/com.example.ijangmin.stock W/System.err: at com.example.ijangmin.stock.RegisterActivity$1$1.onResponse(RegisterActivity.java:51)
04-30 15:16:40.653 6063-6063/com.example.ijangmin.stock W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
04-30 15:16:40.654 6063-6063/com.example.ijangmin.stock W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
04-30 15:16:40.654 6063-6063/com.example.ijangmin.stock W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
04-30 15:16:40.654 6063-6063/com.example.ijangmin.stock W/System.err: at android.os.Handler.handleCallback(Handler.java:790)
04-30 15:16:40.655 6063-6063/com.example.ijangmin.stock W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 15:16:40.655 6063-6063/com.example.ijangmin.stock W/System.err: at android.os.Looper.loop(Looper.java:164)
04-30 15:16:40.656 6063-6063/com.example.ijangmin.stock W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6494)
04-30 15:16:40.656 6063-6063/com.example.ijangmin.stock W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-30 15:16:40.656 6063-6063/com.example.ijangmin.stock W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
04-30 15:16:40.656 6063-6063/com.example.ijangmin.stock W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)