2017-05-04 144 views
-1

我想用电子邮件和密码登录数据库并获取信息。我不知道我的问题是什么,因为当点击登录按钮时, 什么也没有显示。错误是:java.lang.String无法转换为JSONObject 2

*org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject*. 

我的登录代码:

Button SignIn = (Button) findViewById(R.id.btn_signIn); 
final EditText etEmail = (EditText) findViewById(R.id.etMail); 
final EditText etPassword = (EditText) findViewById(R.id.etPassword); 

SignIn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     email = etEmail.getText().toString(); 
     password = etPassword.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) { 
         String name = jsonResponse.getString("name"); 
         String surname = jsonResponse.getString("surname"); 

         Intent intent = new Intent(LoginActivity.this, UsersMainActivity.class); 
         intent.putExtra("name", name); 

         LoginActivity.this.startActivity(intent); 
        } else { 
         AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this); 
         builder.setMessage("Login Failed Please Try again") 
           .setNegativeButton("Retry", null) 
           .create() 
           .show(); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 

     }; 

     LoginRequest loginRequest = new LoginRequest(email, password, responseListener); 
     RequestQueue queue = Volley.newRequestQueue(LoginActivity.this); 
     queue.add(loginRequest); 
    } 
}); 

我的PHP代码:

<?php 
$con = mysqli_connect("localhost","id1519330","****","id1519330"); 

    $email = $_POST["email"]; 
    $password = $_POST["password"]; 
    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ? and password = ? "); 

    mysqli_stmt_bind_param($statement, "ss", $email,$password); 
    mysqli_stmt_execute($statement); 
    mysqli_stmt_store_result($statement); 
    mysqli_stmt_bind_result($statement, $id, $name, $surname, $email, $password); 

    $response = array(); 
    $response["success"] = false; 

while(mysqli_stmt_fetch($statement)){ 

      $response["success"] = true; 
      $response["name"] = $name; 
      $response["surname"] = $surname; 
      $response["email"] = $email; 
      $response["password"] = $password; 

    } 
    echo json_encode($response); 
?> 
+0

值添加一个弹出窗口或控制台的println以确保密码和电子邮件都是字符串,而不是JSON对象第一。之后,您需要检查实际响应的内容并打印出整个对象。你在某处解析某些错误。错误信息本身实际上应该告诉你你解析错误,但你没有包括整个东西, – JordanGS

+0

屏幕右侧有'Related'部分。大量同样的问题得到解答。 –

回答

0

好像你的反应是给一个HTML代码(发生当你的请求没有给出状态200.通过使用调试器或打印检查你的字符串响应回答问题,你就可以知道你的请求有什么问题。

0

它缝是你response是没有正确的json风格来解析。请参阅this page 更多的细节,并尝试调试,看看这个是什么response

相关问题