1
In the small project I am doing, the user enters his login and password. The system makes a POST for a URL and checks the answer. If you log in, it returns some things like the user ID, email and everything else.
I need to get the user ID so I can subscribe to an MQTT topic, but I’m having trouble getting this ID! I’ve been able to map and separate from JSON, but when I try to use it I’m always getting zero. I will post the classes below (at least the relevant parts).
Class responsible for doing the post (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’s logged in, and if not logged in, 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 error is effective (this method is in another class, but I’ll 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 received 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 don’t know how to solve (I was supposed to get 195)
Right, but then I would have to authenticate already in the MQTT connection method, right? The idea was that it would only try to connect to MQTT if logged in. This method (authenticatLogin) is what makes takes the typed data to send by POST and everything else...
– FearX
My area is not very Java, sorry if I’m talking nonsense... But by logic your variables are coming as
NULL
because they are not being fed, they are only fed when executed the methodautenticaLogin
, then if it will only give the POST if you log into the MQTT you need to see some way to feed them.– Matheus Ribeiro
Yes, that’s what’s happening. I guess I still can’t figure out the right way to do it, if there’s anything specific that can help me. Thanks for trying, let’s see if anyone else shows up...
– FearX
You’re welcome, soon as someone who understands well comes as a solution!
– Matheus Ribeiro