volley.ParseError - java.lang.String can not be converted

0

Hello,

I'm sending POST to a PHP file using the Volley library. It all happens well until the application tries to show the response to the user. In the PHP file I use the echo 'user_created'; command. The application has the following error:

Error: com.android.volley.ParseError: org.json.JSONException: Value created_user of type java.lang.String can not be converted to JSONObject

I'm sorry for not being logcat, but I'm testing the application on a real cell.

Here is the code I use to send 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);
    
asked by anonymous 04.01.2016 / 07:40

1 answer

1

I will explain the error: this is because you are not receiving the response from the server in JSON format but rather a string written "strong_user" and so the com.android error occurs. volley.ParseError: org.json.JSONException: Value created_user of type java.lang.String can not be converted to JSONObject
Possible Solutions:
1- You schedule your server to respond to an example JSON

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

2- You use the Volleyball StringRequest follows the Doc Volley

    
04.01.2016 / 18:38