0

I have run into an issue with Volley String/JsonObject requests.

When I try to login, I want to pass json-data as parameters, but what I want to receive is a string. This causes issues because when using JsonObject request, it requires the data to be that of an JsonObject, and because the return type is of type java.lang.string it instead complains that it cannot convert type java.lang.string to jsonObject. Fine. When trying String request, the params can't be jsonObject, so that won't work... at all.

What do I do? In short I want to be able to pass JsonObject parameters and retrieve a string, is this possible?

Here's some code to help you understand my situation:

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

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = 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 " + response ,Toast.LENGTH_LONG).show();
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
                Log.d("TAG" ,error.toString());
                failedLogin();

            }
        });
requestQueue.add(jsonObjectRequest);

This prompts the error

com.android.Volley.ParseError: org.json.JSONException: Value (STRING TOKEN) of type java.lang.String cannot be converted to JsonObject

and with string request you can't pass jsonObject with getParams, unfortunately!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Hoaz
  • 77
  • 1
  • 10
  • You should have edited your post from yesterday instead of essentially reposting – OneCricketeer May 23 '17 at 21:45
  • Possible duplicate of [How to send a POST request using volley with string body?](https://stackoverflow.com/questions/33573803/how-to-send-a-post-request-using-volley-with-string-body) – OneCricketeer May 23 '17 at 21:49
  • sorry @cricket_007 I am new to posting, not entirely sure if the post will be seen by others if I edit! But I'll keep this in mind next time! and I figured they were two slightly different questions, but I see your point – Hoaz May 23 '17 at 21:49
  • When you edit a question, it'll be bumped to the front page – OneCricketeer May 23 '17 at 21:50

0 Answers0