I need to deserialize JSON to a generic list, but I'm having an error that I believe to be in the conversion:
Method call:
AtualizarJSON at = (AtualizarJSON) DeserializaConsulta(AtualizarJSON.class, resultadoJSON);
Method:
private <T> List<T> DeserializaConsulta(Class<T> tipo, String resultadoJSON) throws JSONException {
if (resultadoJSON != null) {
return Arrays.asList(new Gson().fromJson(resultadoJSON, tipo));
}
return null;
}
RequestinWS:
privateStringConsultarOuBaixarAtualizacoes(StringurlT)throwsIOException{InputStreamis=null;try{URLurl=newURL(urlT);HttpURLConnectionconn=(HttpURLConnection)url.openConnection();conn.setReadTimeout(10000);conn.setConnectTimeout(15000);conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
conn.getResponseCode();
is = conn.getInputStream();
Reader reader = null;
reader = new InputStreamReader(is, "UTF-8");
char[] buffer = new char[2048];
reader.read(buffer);
return new String(buffer);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient (true) to accept malformed JSON at line 1 column 126
I think this solution is not ideal, there must be a way to solve this problem by setting the types as I have seen in some examples, but I could not find examples for this case where I pass the type by parameter. >