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)