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)