1. Are you ready for the Galaxy S20? Here is everything we know so far!

Json error: Value <br of type java.lang.String cannot be converted to JSONObject

Discussion in 'Android Development' started by gmrcs, Jun 12, 2020.

  1. gmrcs

    gmrcs Lurker
    Thread Starter

    java code
    Code (Text):
    1.  
    2. btn_log.setOnClickListener(new View.OnClickListener(){
    3.     @Override
    4.     public void onClick(View v) {
    5.  
    6.         final String username=tv_usuario.getText().toString();
    7.         final String password=tv_password.getText().toString();
    8. //51
    9.         Response.Listener<String> responseListener = new Response.Listener<String>() {
    10.             @Override
    11.             public void onResponse(String response) {
    12.                 try {
    13. //55
    14.                     JSONObject jsonResponse = new JSONObject(response);
    15.                     boolean success = jsonResponse.getBoolean("success");
    16.                     if(success){
    17.                         String name = jsonResponse.getString("name");
    18.                         int age = jsonResponse.getInt("age");
    19.  
    20.                         Intent intent = new Intent(MainActivity.this, Usuario.class);
    21.                         intent.putExtra("name", name);
    22.                         intent.putExtra("username", username);
    23.                         intent.putExtra("age", age);
    24.                         intent.putExtra("password", password);
    25.  
    26.                         MainActivity.this.startActivity(intent);
    27.  
    28.  
    29.                     }else{
    30.                         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    31.                         builder.setMessage("Error Login")
    32.                                 .setNegativeButton("Retry",null)
    33.                                 .create().show();
    34.                     }
    35.                 } catch (JSONException e) {
    36.                     e.printStackTrace();
    37.                 }
    38.             }
    39.         };
    40.         LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
    41.         RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    42.         queue.add(loginRequest);
    43.     }
    44. });
    45.  
    Login
    Code (Text):
    1.  
    2. public class LoginRequest extends StringRequest {
    3.     private static final String LOGIN_REQUEST_URL="http://localhost/Login.php";
    4.     private Map<String,String> params;
    5.     public LoginRequest(String username,String password, Response.Listener<String> listener){
    6.         super(Request.Method.POST, LOGIN_REQUEST_URL,listener, null);
    7.         params = new HashMap<>();
    8.         params.put("username", username);
    9.         params.put("password", password);
    10.     }
    11.  
    12.     @Override
    13.     public Map<String, String> getParams() {
    14.         return params;
    15.     }
    16.  
    17. }
    18.  
    here is my php
    Code (Text):
    1.  
    2. hp
    3.     $con = mysqli_connect("localhost", "root", "", "usuarios");
    4.  
    5.     $username = $_POST["username"];
    6.     $password = $_POST["password"];
    7.  
    8.     $statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
    9.     mysqli_stmt_bind_param($statement, "ss", $username, $password);
    10.     mysqli_stmt_execute($statement);
    11.  
    12.     mysqli_stmt_stone_result($statement);
    13.     mysqli_stmt_bind_result($statement, $userID, $name, $username, $password, $age);
    14.  
    15.     $response = array();
    16.     $response["success"] = false;
    17.  
    18.     while(mysqli_stmt_fetch($statement)){
    19.         $response["success"] = true;
    20.         $response["name"] = $name;
    21.         $response["age"] = $age;
    22.         $response["username"]  = $username;
    23.         $response["password"] = $password;
    24.     }
    25.  
    26.     echo json_encode($response);
    27.  
    28. ?>
    29.  
    Logcat view
    Code (Text):
    1.  
    2. W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
    3. W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
    4.         at org.json.JSONObject.<init>(JSONObject.java:163)
    5. W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:176)
    6.         at com.example.avance.MainActivity$2$1.onResponse(MainActivity.java:55)
    7.         at com.example.avance.MainActivity$2$1.onResponse(MainActivity.java:51)
    8.         at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
    9. W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
    10.         at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
    11. W/System.err:     at android.os.Handler.handleCallback(Handler.java:789)
    12.         at android.os.Handler.dispatchMessage(Handler.java:98)
    13.         at android.os.Looper.loop(Looper.java:164)
    14. W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6944)
    15.         at java.lang.reflect.Method.invoke(Native Method)
    16.         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    17. W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
    18.  
     


  2. wseng92

    wseng92 Android Enthusiast

    try print out jsonResponse. What value you get?
     
Loading...
Similar Threads - Json error Value
  1. beginnerProgrammer
    Replies:
    0
    Views:
    419
  2. Dinakar
    Replies:
    0
    Views:
    407
  3. anirudha
    Replies:
    0
    Views:
    394
  4. Rascalsailor
    Replies:
    2
    Views:
    437
  5. Chin Chun Seong
    Replies:
    0
    Views:
    362
  6. Korsholm
    Replies:
    0
    Views:
    256
  7. Jay Roa
    Replies:
    2
    Views:
    3,489
  8. mark9
    Replies:
    5
    Views:
    493
  9. darth_daniel
    Replies:
    9
    Views:
    1,232
  10. nagamothu
    Replies:
    30
    Views:
    1,783

Share This Page

Loading...