2017-08-11 36 views
0

我的应用程序的一部分工程更新在线用户数据库来更改他们的名称,并且有关'更新'功能的PHP代码似乎正常工作,名称更改数据库。然而,部分旨在将响应传递回应用程序以使其可以执行其他操作的代码无法正常工作,因此该应用程序并未执行一系列重要操作。Android的php:字符串不能转换为JSONObject

我使用Volley进行服务器通信,如果有帮助的话。

我需要这样做,以便一旦PHP文件成功更新表中的名称字段(它已经在做),它会正确地将响应传回给应用程序,以便我可以执行一些命令。

下面是与接口等:

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); 

     } 
    }); 

} 

名称更改请求类的活动:

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; 
} 

php文件,存储在数据库服务器上(在线):

<?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); 

回答

1

看起来像是在PHP代码中打印response两次。

刚从的if/else条件中的回声语句,只打印响应一次:

<?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; 
    //remove this: 
    //echo json_encode($response); 
} else { 
    $response["success"] = false; 
    //remove this: 
    //echo json_encode($response); 
} 

echo json_encode($response); 
+0

我试过了一段时间后(并只试了一次),但问题仍然存在。它仍然将数据传递给服务器并更新表,但显示的是同样的'无法转换为JSONObject'错误。我不确定老实说有什么问题 – JSteward

+0

那么只要看看服务器发回给你的是什么。在onResponse()回调的顶部放置一条日志语句。 –

相关问题