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.
Leonardo Figueiredo, thank you very much for your answer, solved my problem.
– Q.Wesley