HTTP GET in JAVA with a different result than POSTMAN

Asked

Viewed 121 times

0

I am running an HTTP Get Request in Java using Gson to use the data in my program, but the first and last attribute are coming as null. However, when performing the request in POSTMAN I saw that really the result is wrong.

Follow my request code in JAVA and the answers:

try{
            String url = "https://api.codenation.dev/v1/challenge/dev-ps/generate-data?token=MEUTOKEN";

            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

            conn.setRequestMethod("GET");

            if (conn.getResponseCode() != 200) {
                System.out.println("Erro " + conn.getResponseCode() + " ao obter dados da URL " + url);
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = br.readLine()) != null) {
                response.append(inputLine);

            }
            br.close();
            conn.disconnect();

            Gson gson = new Gson();
            Mensagem mensagem = gson.fromJson(response.toString(),Mensagem.class);

            System.out.println("CASAS: "+ mensagem.getNum_casas());
            System.out.println("TOKEN: " + mensagem.getToken());
            System.out.println("CIFRADO: "+ mensagem.getCifrado());
            System.out.println("DESCIFRADO: "+ mensagem.getDecifrado());
            System.out.println("Resumo: "+ mensagem.getResumo_criptográfico());

        }catch (IOException ex){
            ex.printStackTrace();
        } 

Message class

public class Mensagem {

    private String num_casas;
    private String token;
    private String cifrado;
    private String decifrado;
    private String resumo_criptográfico;

    public Mensagem(String num_casas, String token, String cifrado, String decifrado, String resumo_criptográfico) {
        this.num_casas = num_casas;
        this.token = token;
        this.cifrado = cifrado;
        this.decifrado = decifrado;
        this.resumo_criptográfico = resumo_criptográfico;
    }

    public String getNum_casas() {
        return num_casas;
    }

    public void setNum_casas(String num_casas) {
        this.num_casas = num_casas;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getCifrado() {
        return cifrado;
    }

    public void setCifrado(String cifrado) {
        this.cifrado = cifrado;
    }

    public String getDecifrado() {
        return decifrado;
    }

    public void setDecifrado(String decifrado) {
        this.decifrado = decifrado;
    }

    public String getResumo_criptográfico() {
        return resumo_criptográfico;
    }

    public void setResumo_criptográfico(String resumo_criptográfico) {
        this.resumo_criptográfico = resumo_criptográfico;
    }

    @Override
    public String toString() {
        return "Mensagem{" +
                "num_casas=" + num_casas +
                ", token='" + token + '\'' +
                ", cifrado='" + cifrado + '\'' +
                ", decifrado='" + decifrado + '\'' +
                ", resumo_criptográfico='" + resumo_criptográfico + '\'' +
                '}';
    }
}

Upshot:

CASAS: null
TOKEN: MEU TOKEN
CIFRADO: ahsr pz jolhw, zovd tl aol jvkl! spubz avychskz
DESCIFRADO: 
Resumo: null

But when the request is made in POSTMAN, the result is:

{
    "numero_casas": 7,
    "token": "MEUTOKEN",
    "cifrado": "ahsr pz jolhw, zovd tl aol jvkl! spubz avychskz",
    "decifrado": "",
    "resumo_criptografico": ""
}

Someone could help me find out pq the first and last line are coming out as null ? I believe it’s some detail with the Bufferedreader I didn’t notice.

1 answer

2


Field name in Message class does not match field name in JSON.

num_casas vs. numero_casas

The same for the other field that is coming null, the problem is the accent.

Browser other questions tagged

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