0

I am trying to fetch data from String Response from the server (Server backend running on PHP and sending a JsonObject response) and then converting it into JsonObject to fetch data correctly. But Android Volley showing the error below :

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

My code is given below

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

    } 

And the PHP code is :

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




  ?> 
XpressGeek
  • 2,889
  • 3
  • 23
  • 27
  • have you checked the response in postman? – Navneet Krishna Jun 29 '18 at 12:16
  • Possible duplicate of [JSONException: Value of type java.lang.String cannot be converted to JSONObject](https://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject) – Jinesh Malavia Jun 29 '18 at 12:16
  • 1
    **Never store user passwords in clear text.** Only store password hashes. Use `password_hash()` to generate the hash and `password_verify()` to verify a password against a stored hash. – M. Eriksson Jun 29 '18 at 12:18
  • 1
    You are also _wide open_ to SQL Injections and should use Prepared Statements with placeholders instead of manually injecting (totally unescaped) user data like that into a query. – M. Eriksson Jun 29 '18 at 12:21
  • The problem is simple, you're expecting a json formatted response, but you're not getting it, you're getting html or any other non-json string in the response, must likely to be a php error message. Try to Log.d(ServerResponse) before you try to transform it to JSONObject, or use a breakpoint, or use PostMan to make a request and see the actual response. – FerDensetsu Jul 04 '18 at 21:28
  • Thanks, @FerDensetsu, I was printing "Database connected successfully" message with the JSON response, that's why the Android side is not receiving a pure JSON response but also the string. So it gets confused. Now I commented the database connection message line and problem solved. – XpressGeek Jul 13 '18 at 14:27

0 Answers0