Receiving wrong id

1

In the small project that I'm doing, the user enters his login and password. The system posts to a URL and checks the response. If you log in, it returns some things like user ID, email and everything.

I need to get the user ID to be able to sign up for an MQTT topic, but I'm having trouble getting this ID! I have already been able to map and detach from JSON, but when I try to use it, I always get zero. I'll post the classes below (at least the relevant parts).

Class responsible for posting (when called):

public class SenderPost {
    private HttpResponse response;

    public int getIdReceived() {
        return idReceived;
    }

    private void setIdReceived(int idReceived) {
        this.idReceived = idReceived;
    }

    private int idReceived;

    int postLogin(String login, String password) throws UnsupportedEncodingException {

       //(código omitido) configurações do POST

        try {
            response = client.execute(post);
            HttpEntity entidade = response.getEntity();
            String responseString = EntityUtils.toString(entidade, "UTF-8");
            DOMInfoLogin dil = new ObjectMapper().readValue(responseString, DOMInfoLogin.class); // aqui eu mapeio o JSON pra poder usar os atributos
            setIdReceived(dil.getId()); //aqui eu coloco o atributo idReceived como o id que recebo do JSON

      }
       //(código omitido) tratativas de erro 

        return response.getStatusLine().getStatusCode();
    }
}

This is the class that runs SenderLogin and checks its response (to see if it has logged in, and if not, why):

public class AutenticaLogin {

    public SenderPost post = new SenderPost();

    boolean autenticaLogin(TextField txtID, PasswordField pwdField, Button btnLogin) throws IOException {
        btnLogin.setDisable(false);
        switch (post.postLogin(txtID.getText(), pwdField.getText())) {
            case 200:
                return true;
//(código omitido) Tratativas para outros status code
        }
    }
}

Finally, the method where the actual error is (this method stays in another class, but I will omit it and leave only the method): I try to concatenate my String with the information and then I get it. (Before using the set method, I was getting NPE)

 private AutenticaLogin auth = new AutenticaLogin();

    public void connect() throws MqttException, IOException {
    //(código omitido) Configurações do MQTT
    String topic = "topico/"+auth.post.getIdReceived()+"/start";
//(código omitido) Conexões do MQTT
}

And here in the debug is the problem, but I do not know how to solve (it was to receive 195)

    
asked by anonymous 27.11.2018 / 13:28

2 answers

0

The solution was to create static attribute and set method, and when I get the ID, assignment in the class. I do not know if it's the ideal, but it works fine for what I need.

private static String getIdReceived() {
    return idReceived;
}
switch (post.postLogin(txtID.getText(), pwdField.getText())) {
            case 200:
                MqttBase.setIdReceived(post.getIDClient());
                MqttBase.setEmpresaReceived(post.getIDEmpresa());
                return true;
//código omitido
    
08.01.2019 / 11:58
0

You have been asked to call the autenticationLogin method to feed your variable idReceived of class SenderPost

 private AutenticaLogin auth = new AutenticaLogin();

    public void connect() throws MqttException, IOException {
    //(código omitido) Configurações do MQTT

    auth.autenticaLogin(txtID, pwdField, btnLogin);

    String topic = "topico/"+auth.post.getIdReceived()+"/start";
//(código omitido) Conexões do MQTT
}

If you do not call autenticationLogin you will never call postLogin , which in turn will feed the idReceived variable.

    
27.11.2018 / 13:39