How to deserialize JSON using Gson with generic list?

3

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

asked by anonymous 18.11.2014 / 00:48

1 answer

3

I think these invalid characters come from the way they are reading, not from encoding as I supposed at first.

When you allocate a char array with 2048 positions and use it to read the response. It probably has 60% of characters being previous values in the heap memory. And this causes the parsing problem of the GSON.

I recommend reading the response from the server using a BufferedReader . Reading all the lines until the end of the answer.

The code would be:

private String ConsultarOuBaixarAtualizacoes(String urlT) throws IOException {
    InputStream is = null;
    BufferedReader reader = null;

    try {
        URL url = new URL(urlT);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect();
        conn.getResponseCode();

        is = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        StringBuilder sb = new StringBuilder();
        String line = null;

        // Le cada linha da resposta ate o final
        while((line = reader.readLine()) != null) {
            sb.append(line);
        }

        return sb.toString();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
    
18.11.2014 / 02:05