0

I am trying to log onto my app using volley requests, however, I get the following error

"com.android.volley.ParseError: org.json.JSONException: Value Bad of type java.lang.String cannot be converted to JSONobject"

when trying to login.

Here's the code for the volley request:

  Map<String, String> postParam = new HashMap<String, String>();
        postParam.put("name", mEmail);
        postParam.put("password", mPassword);

            //final JSONObject jsonBody = new JSONObject("{\"name\":\""+ mEmail +"\", \"password\":\"" + mPassword + "\"}");
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.POST, "http://" + getString(R.string.user_login), new JSONObject(postParam),
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                                Toast.makeText(LoginActivity.this,"success!!",Toast.LENGTH_LONG).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
                            failedLogin();

                        }
                    });
            requestQueue.add(jsonObjRequest);

I've tried the code using requests in python, including both name and password as Json parameters, like this:

post(address, data = json.dumps({"name": "alex", "password": "XXXX"}))

this works and returns 200 (OK!). It just doesn't work with android-volley.

Any help is appreciated!

EDIT:
I tried StringRequest instead of JSONRequest and instead got this error

E/Volley: [3835] BasicNetwork.performRequest: Unexpected response code 400 for ADDR
D/TAG: com.android.volley.ServerError

EDIT 2:
I managed to get more out of LogCat this time

W/System.err: org.json.JSONException: Value (JSONObject.java:160) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at org.json.JSONObject.(JSONObject.java:173) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at com.example.envi.LoginActivity$UserLoginTask$2.onErrorResponse(LoginActivity.java:364) 05-22 11:09:30.255 18612-18612/com.example. envi W/System.err: at com.android.volley.Request.deliverError(Request.java:564) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at android.os.Handler.handleCallback(Handler.java:751) 05-22 11:09:30.255 18612-18612/com.example. W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at android.os.Looper.loop(Looper.java:154) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6290) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at java.lang.reflect.Method.invoke(Native Method) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 05-22 11:09:30.255 18612-18612/com.example.envi W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Hoaz
  • 77
  • 1
  • 10
  • Hey, how did you receive the json data. is that includes array and json. [{},{}] or {{},{}} – Visal Varghese May 22 '17 at 07:40
  • Your `response` isn't a proper JSON Object, seems. Add the full logcat – OneCricketeer May 22 '17 at 07:40
  • I think the response you are receiving is of type String, and not a JSONObject. – Meenal May 22 '17 at 07:46
  • I tried doing a StringRequest instead but got a com.android.Volley.ServerError instead. Here's the logcat: E/Volley: [3835] BasicNetwork.performRequest: Unexpected response code 400 for – Hoaz May 22 '17 at 08:07
  • Try doing a post request to http://httpbin.org/post . This can help you check which side of your code has problem – vanna May 22 '17 at 08:21
  • @vanna That worked. So it's not the code then, because what I found out is that while the server returns 200 OK in python, it does not return a response.content, that throws a 500 server error. However it only does that for POST. With GET method it returns response.content. – Hoaz May 22 '17 at 08:35
  • Possible duplicate of [Android:Volley String Request with JsonObject Params](https://stackoverflow.com/questions/44145309/androidvolley-string-request-with-jsonobject-params) – OneCricketeer May 23 '17 at 21:44

0 Answers0