0

My java code is as follows, when the user fills the required fields then given a response from the server whether the user exists or not.

             else {
                    StringRequest stringRequest = new StringRequest(Request.Method.POST,
                            SERVER_URL, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                String code = jsonObject.getString("code");
                                String message = jsonObject.getString("message");
                                builder.setTitle("Server response");
                                builder.setMessage(message);
                                displayAlert(code);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("name", Username);
                            params.put("email", Email);
                            params.put("password", Passwrd);

                            return params;
                        }

                        @Override
                        public RetryPolicy getRetryPolicy() {
                            setRetryPolicy(new DefaultRetryPolicy(
                                    5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                            return super.getRetryPolicy();
                        }
                    };
                    MySingleton.getInstance(Register.this).addToRequestQueue(stringRequest);
                }
            }
        }
    });

}

and the jsonObject response I need is

{
    "code": "reg failed",
    "message": "user exists"
}
  • 4
    So what is the question? – Xenolion Dec 16 '17 at 12:20
  • am getting an error when retriving server response org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject @Xenolion – Neston Marica Dec 16 '17 at 12:40
  • What does the error looks like!? – Xenolion Dec 16 '17 at 12:48
  • Duplicate of [https://stackoverflow.com/questions/10267910/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) – Nikhil Lotke Dec 16 '17 at 12:52
  • org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject @Xenolion – Neston Marica Dec 16 '17 at 12:54
  • Then it is a JSONException and this is because the string you are getting is not what is expected and so try debbuging by Logging add this line `Log.e("The string is:",response);` just before the `try` inside `onResponse` this will make the app to print the value of string import first if not yet. So you will see a log in red showing you how the string looks like before the exception in logs. Check how the string looks like? – Xenolion Dec 16 '17 at 13:02

1 Answers1

0

If the response you wish to get is a JSONObject as you state in your question then you should do a JSONRequest and not a StringRequest.

See this https://developer.android.com/training/volley/request.html

joao86
  • 2,056
  • 1
  • 21
  • 23