我试图从服务器的 String Response 中获取数据(在PHP上运行服务器后端并发送JsonObject响应),然后将其转换为 JsonObject 以获取数据正确地。但是 Android Volley 显示以下错误:
org.json.JSONException:类型java.lang.String的值db无法转换为JSONObject
我的代码如下
public void loginFunction(String a, String b) {
progressDialog.setMessage("Please Wait, We are Inserting Your Data on Server");
progressDialog.show();
final String user_email_insert = a;
final String user_password_insert = b;
String HttpUrl = getString(R.string.server_name)+getString(R.string.log_in_api);
StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String ServerResponse) {
progressDialog.dismiss();
try {
JSONObject person = new JSONObject(ServerResponse);
String name = person.getString("user_full_name");
String email = person.getString("user_email");
Log.d("1111",person.toString());
Toast.makeText(getApplicationContext(), name+"\n"+email, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
progressDialog.dismiss();
Toast.makeText(LogIn.this, volleyError.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_email", user_email_insert);
params.put("user_password", user_password_insert);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(LogIn.this);
requestQueue.add(stringRequest);
}
PHP代码是:
<?php
include "db_connection.php";
$user_email = $_POST["user_email"];
$user_password = $_POST["user_password"];
$sql = "SELECT user_password,user_full_name,user_email FROM user_info WHERE user_email ='" .$user_email. "'";
mysqli_query($con,'SET CHARACTER SET utf8');
mysqli_query($con,"SET SESSION collation_connection ='utf8_general_ci'");
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_row($res);
if($user_password == $row[0]){
// echo "Login successfull".$row[1];
$myObj->user_full_name = $row[1];
$myObj->user_email = $row[2];
$myJSON = json_encode($myObj,JSON_UNESCAPED_UNICODE);
echo $myJSON;
//echo "Login successfull";
}else{
echo "Wrong password";
//header( 'Location: login_failed.php' ) ;
}
mysqli_close($con);
?>