Android php:无法将字符串转换为JSONObject
我的应用程序的一部分用来更新在线用户数据库以更改其名称,而关于“更新”功能的php代码似乎工作正常,数据库中的名称也在更改。然而,用于将响应传递回应用程序以便它可以执行其他操作的部分代码没有正常工作,因此应用程序没有执行一系列重要操作 如果有帮助的话,我正在使用截击进行服务器通信 我需要这样做,一旦php文件成功地更新了表中的名称字段(它已经这样做了),它就会正确地将响应传递回应用程序,以便我可以执行一些命令 以下是带有界面等的活动:Android php:无法将字符串转换为JSONObject,php,android,mysqli,Php,Android,Mysqli,我的应用程序的一部分用来更新在线用户数据库以更改其名称,而关于“更新”功能的php代码似乎工作正常,数据库中的名称也在更改。然而,用于将响应传递回应用程序以便它可以执行其他操作的部分代码没有正常工作,因此应用程序没有执行一系列重要操作 如果有帮助的话,我正在使用截击进行服务器通信 我需要这样做,一旦php文件成功地更新了表中的名称字段(它已经这样做了),它就会正确地将响应传递回应用程序,以便我可以执行一些命令 以下是带有界面等的活动: public class PopupNameChange e
public class PopupNameChange extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_up_change_name);
final Button nameChangeButton = (Button) findViewById(R.id.confirmButton);
final EditText newName = (EditText) findViewById(R.id.etNewName);
final String userNameFromPref = UserCredentials.getLoggedInUserName(getApplicationContext());
final String userEmailFromPref = UserCredentials.getLoggedInEmailUser(getApplicationContext());
TextView userNameCurrent = (TextView) findViewById(R.id.tvCurrentName);
userNameCurrent.setText(userNameFromPref);
getWindow().setLayout(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
nameChangeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = newName.getText().toString();
final String email = userEmailFromPref.toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
System.out.println("Print test");
*** the logcat error points at the line below ***
JSONObject jsonResponse = new JSONObject(response);
System.out.println(jsonResponse);
boolean success = jsonResponse.getBoolean("success");
if (success) {
UserCredentials.setLoggedInUserName(getApplicationContext(), name);
AlertDialog.Builder alertMessage = new AlertDialog.Builder(PopupNameChange.this);
alertMessage.setMessage("Name changed to: " + name)
.setNegativeButton("Close", null)
.create()
.show();
} else {
AlertDialog.Builder alertMessage = new AlertDialog.Builder(PopupNameChange.this);
alertMessage.setMessage("Name change failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
NameChangeRequest nameChangeRequest = new NameChangeRequest(email, name, responseListener);
RequestQueue queue = Volley.newRequestQueue(PopupNameChange.this);
queue.add(nameChangeRequest);
}
});
}
公共类PopupNameChange扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.pop\u up\u change\u name);
最终按钮名称ChangeButton=(按钮)findViewById(R.id.confirButton);
final EditText newName=(EditText)findViewById(R.id.etNewName);
最后一个字符串userNameFromPref=UserCredentials.getLoggedInUserName(getApplicationContext());
最后一个字符串userEmailFromPref=UserCredentials.getLoggedInEmailUser(getApplicationContext());
TextView用户名当前=(TextView)findViewById(R.id.tvCurrentName);
userNameCurrent.setText(userNameFromPref);
getWindow().setLayout(ConstraintLayout.LayoutParams.WRAP_内容,ConstraintLayout.LayoutParams.WRAP_内容);
getWindow().setBackgroundDrawable(新的ColorDrawable(Color.TRANSPARENT));
nameChangeButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
最终字符串名称=newName.getText().toString();
最终字符串email=userEmailFromPref.toString();
Response.Listener responseListener=新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
系统输出打印项次(“打印测试”);
***logcat错误点位于下面的行***
JSONObject jsonResponse=新的JSONObject(响应);
System.out.println(jsonResponse);
boolean success=jsonResponse.getBoolean(“success”);
如果(成功){
UserCredentials.setLoggedInUserName(getApplicationContext(),name);
AlertDialog.Builder alertMessage=新建AlertDialog.Builder(PopupNameChange.this);
setMessage(“名称更改为:”+名称)
.setNegativeButton(“关闭”,null)
.create()
.show();
}否则{
AlertDialog.Builder alertMessage=新建AlertDialog.Builder(PopupNameChange.this);
alertMessage.setMessage(“名称更改失败”)
.setNegativeButton(“重试”,null)
.create()
.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
};
NameChangeRequest NameChangeRequest=新的NameChangeRequest(电子邮件、姓名、响应者);
RequestQueue=Volley.newRequestQueue(PopupNameChange.this);
添加(名称更改请求);
}
});
}
更改名称请求类:
public class NameChangeRequest extends StringRequest{
private static final String NameChange_Request_URL = "http://app-user-base.000webhostapp.com/NameChange.php";
private Map<String, String> params;
public NameChangeRequest(String email, String name, Response.Listener<String> listener) {
super(Request.Method.POST, NameChange_Request_URL, listener, null);
params = new HashMap<>();
params.put("email", email);
params.put("name", name);
}
@Override
public Map<String, String> getParams() {
return params;
}
公共类名称更改请求扩展了StringRequest{
私有静态最终字符串名称更改\u请求\u URL=”http://app-user-base.000webhostapp.com/NameChange.php";
私有映射参数;
公共名称更改请求(字符串电子邮件、字符串名称、响应.侦听器){
super(Request.Method.POST,NameChange\u Request\u URL,listener,null);
params=新的HashMap();
参数put(“电子邮件”,电子邮件);
参数put(“名称”,名称);
}
@凌驾
公共映射getParams(){
返回参数;
}
php文件,存储在数据库服务器上(联机):
看起来您在PHP代码中打印了两次响应
只需从if/else条件中删除echo语句,然后只打印一次响应:
我不久前就试过了(只是又试了一次)但问题依然存在。它仍在向服务器传递数据并更新表,但显示了相同的“无法转换为JSONObject”错误。我不确定它到底出了什么问题。那么,请仔细查看服务器发送回您的内容。在onResponse()回调的顶部放置一条log语句。
<?php
$con = mysqli_connect("localhost", "dbUsername", "dbPassword", "dbName");
$email = $_POST["email"];
$name = $_POST["name"];
$statement = "UPDATE users SET name = '$name' WHERE email = '$email'";
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
$response = array();
$response["success"] = false;
if(mysqli_query($con,$statement)) {
$response["success"] = true;
echo json_encode($response);
} else {
$response["success"] = false;
echo json_encode($response);
}
echo json_encode($response);