Error while receiving json from Android Studio objects

Asked

Viewed 141 times

0

Good afternoon guys, I’m having trouble getting an array of objects and show in Listview on android. In case the class that is giving error is this:

public class HTTPCarregaLista extends AsyncTask<Void, Void, List<Documento>> {

private final String filial;

public HTTPCarregaLista(String filial) {
    this.filial = filial;
}

@Override
protected List<Documento> doInBackground(Void... Void) {

    List<Documento> docs = new ArrayList<Documento>();

    StringBuilder resposta = new StringBuilder();

    try {
        URL url = new URL("http://www.meuwebservice.com" + this.filial);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setDoOutput(true);
        connection.setConnectTimeout(5000);
        connection.connect();

        Scanner scanner = new Scanner(url.openStream());
        while(scanner.hasNext()){
            resposta.append(scanner.nextLine());
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return Collections.singletonList(new Gson().fromJson(resposta.toString(), Documento.class));

}

In case the line:

return Collections.singletonList(new Gson().fromJson(resposta.toString(), Documento.class));

Fire this Exception Fire:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

My Class Document:

public class Documento implements Serializable {
private long Cd_Filial;
private String Cd_Cod_Doc;
private String Dt_Movimento;
private String Cd_Cod_Produto;
private String Cd_Localizacao;
private String Nr_Documento;
private String Qtd_Recebida;
private String Qtd_Aceita;
private String Qtd_Vendida;
private String Qtd_Perda;
private String Qtd_Devolver;
private String Qtd_Devolvida;

@Override
public String toString(){
    return "Filial: " + getCd_Filial()
            + "\nCódigo: " + getCd_Cod_Doc()
            + "\nLocalização: " + getCd_Localizacao()
            + "\nData: " + getDt_Movimento();

}

}

And this is the return of my webservice:

[
 {"Cd_Filial":"1","Dt_Movimento":"01/01/2019 00:00:00",
 "Cd_Cod_Doc":"01","Cd_Cod_Produto":"01",
 "Cd_Localizacao":"01","Nr_Documento":"01","Qtd_Recebida":"10,000",
 "Qtd_Aceita":"0,000","Qtd_Vendida":"0,000","Qtd_Perda":"0,000",
 "Qtd_Devolver":"0,000","Qtd_Devolvida":"0,000"}
]

Can anyone say what I am doing wrong or give any hint regarding the problem? I thank from now on anyone who can help.

1 answer

1


Note that in the error it is telling you where the problem is, an Object is expected, but it is receiving an array in json.

Do it this way:

Gson mGson = new Gson();
mDocumento = Arrays.asList(mGson.fromJson(response.body().getAsJsonArray(), Documento[].class));
  • Leonardo Figueiredo, thank you very much for your answer, solved my problem.

Browser other questions tagged

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