4
I’m having trouble reading a JSON in the format:
[{"RESULTADO":"SUCESSO"}]
Webclient.java:
//PARA LER UM JSON, USAMOS A Scanner
Scanner scanner = new Scanner (connection.getInputStream());
String resposta = scanner.next();
return resposta;
Logintask.java:
@Override
protected String doInBackground(ArrayList<Login>... params) {
ArrayList<Login> result = params[0];
String email = result.get(0).getEmail().toString();
String senha = result.get(0).getSenha().toString();
LoginConverter conversor = new LoginConverter();
String json = conversor.converteParaJSON(email, senha);
WebClient client = new WebClient();
String resposta = client.post(json);
return resposta;
}
@Override
protected void onPostExecute(String resposta) {
//Toast.makeText(context, resposta, Toast.LENGTH_LONG).show();
Log.i("LOG", "Teste: " + resposta);
//AQUI ESTÁ RETORNANDO DA SEGUINTE FORMA
//[{"RESULTADO":"SUCESSO"}]
}
I need to take the value of "SUCCESS" to make a:
if (resposta.equals("SUCESSO")) {
Toast.makeText(context, "LOGADO COM SUCESSO!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "ERRO AO LOGAR!", Toast.LENGTH_LONG).show();
}
You have to "parse" that text for objects. A good library for that is google gson. See tutorial below: https://medium.com/@ssaurel/parse-and-write-json-data-in-java-with-gson-a61f8772e786
– António