Volley.Parseerror - java.lang.String cannot be converted

Asked

Viewed 224 times

0

Hello,

I’m sending POST to an archive PHP using the library Volley. Everything goes well until the application tries to show the answer to the user. In the file PHP use the command echo 'user created';. The application shows the following error:

Error: com.android.Volley.Parseerror: org.json.Jsonexception: Value usuario_created of type java.lang.String cannot be converted to Jsonobject

I apologize for not being the logcat, but is that I am testing the application on a real mobile.

Here’s the code I use to send the POST.

    queue = Volley.newRequestQueue(this);
    map.put("nickname", userName);
    request = new JsonObjectRequest(
            Request.Method.POST,
            url,
            new JSONObject(map),
            new Response.Listener<JSONObject>(){
                @Override
                public void onResponse(JSONObject response){
                    Toast.makeText(ctx, "Resposta: " + response, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Toast.makeText(ctx, "Erro: " + error, Toast.LENGTH_LONG).show();
                }
            }
    );
    queue.add(request);
  • Have you checked whether you are receiving a value in JSON format? see this post => http://stackoverflow.com/questions/30196601/volley-string-cannot-be-converted-to-jsonobject

  • I’m not passing in Json format, buddy, I just echo the text. But I tried printing in json format, I also tried to add header as application/json. There were two possible solutions I found online, but none of them helped.

  • was looking here and it is not advisable to use a Jsonobjectrequest to receive a string

  • How it should be done?

1 answer

1


I will explain the error: this is because you are not receiving the server response in JSON format but a String written "user_created" and so the error occurs com.android.Volley.Parseerror: org.json.Jsonexception: Value usuario_created of type java.lang.String cannot be converted to Jsonobject
Possible Solutions:
1- You program your server to respond to an example JSON

echo '{"mensagem": "usuario_criado"}';

2- You use Stringrequest from Volley follow Doc Volley

Browser other questions tagged

You are not signed in. Login or sign up in order to post.