String conversion error in Jsonarray - Volley Android method

Asked

Viewed 179 times

2

My application receives a Jsonstring via web service from a C#API as listed below:

[{
"LimiteCredito": 123.00,
"PessoasFuncionarioId": 16,
"Id": 12,
"Nome": "Cliente PF A",
"DataEdicao": "\/Date(1485019536610-0200)\/",
"UsuarioAlteracaoId": "cdf23118-5991-4eb5-9ec4-ea1fc6e6ce3a",
"RazaoSocial": "Cliente PF A",
"Tipo": 1,
"Cpf": "80158536991   ",
"Sexo": "M",
"TelefonePrincipal": "(41) 9999-9999",
"Email": "[email protected]",
"EstadoRgId": 2,
"Apelido": "Cliente PF A",
"Erp10Id": 1,
"GrupoCrCpId": 1
}]

When trying to convert this JSONString for JSONArray, I get a org.json.JSONException with the following error:

org.json.JSONException: Value [{"LimiteCredito": 123.00,
"PessoasFuncionarioId": 16,
"Id": 12,
"Nome": "Cliente PF A",
"DataEdicao": "\/Date(1485019536610-0200)\/",
"UsuarioAlteracaoId": "cdf23118-5991-4eb5-9ec4-ea1fc6e6ce3a",
"RazaoSocial": "Cliente PF A",
"Tipo": 1,
"Cpf": "80158536991   ",
"Sexo": "M",
"TelefonePrincipal": "(41) 9999-9999",
"Email": "[email protected]",
"EstadoRgId": 2,
"Apelido": "Cliente PF A",
"Erp10Id": 1,
"GrupoCrCpId": 1
}] of type java.lang.String cannot be converted to JSONArray

Below is my code using the Volley library. - Code that calls Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teste_requisicao);

    url = "minhaURL";
    queue = Volley.newRequestQueue(TesteRequisicaoActivity.this);


    callByJsonArrayRequest(null);
}

// CALLS VOLLEY
public void callByJsonArrayRequest(View view) {
    Map<String, String> params = new HashMap<String, String>();
    params.put("UserName", "demo");
    params.put("Password", "12341234");

    RegrasClienteMixForteHelper request = new RegrasClienteMixForteHelper(Request.Method.POST, url, params, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.i("Script", "SUCCESS: " + response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(TesteRequisicaoActivity.this, "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

    request.setTag("tag");
    queue.add(request);
}

@Override
public void onStop() {
    super.onStop();

    queue.cancelAll("tag");
}
  • Code that calls the method of receiving Jsonarray data:

    public class RegrasClienteMixForteHelper extends Request<JSONArray> {
    private Response.Listener<JSONArray> response;
    private Map<String, String> params;
    
    public RegrasClienteMixForteHelper(int method, String url, Map<String, String> params, Response.Listener<JSONArray> response, Response.ErrorListener listener) {
                super(method, url, listener);
                this.params = params;
                this.response = response;}
    
    
    
    
    public Map<String, String> getParams() throws AuthFailureError {
        return params;
    }
    
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("apiKey", "application/json");
    
        return (header);
    }
    
    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
    
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            JSONArray jsonArray = new JSONArray(jsonString);// ERROR -> this is not a JSONArray, same for JSONObject
            return Response.success(jsonArray, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    @Override
    protected void deliverResponse(JSONArray response) {
        this.response.onResponse(response);
    }
    

What’s wrong with the code so you can’t convert to Jsonarray?

  • Cara, Voce can debug and see if the value of the variable jsonString is exactly what it shows there in error? I tested here, and he managed to assemble the JSON cool!

  • Thiago, thank you so much for your test. This way we analyzed the data sent and the problem was in formatting some information sent to the Android device. It was just the date formatting. Now it’s working.

  • Solved your doubt?

No answers

Browser other questions tagged

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