String conversion error in JSONArray - Android Volley Method

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 to 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

Follow my code below 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 I can not convert to JSONArray?

    
asked by anonymous 21.02.2017 / 15:04

0 answers