Convert String to JSON

0

I would like some help to understand this conversion that I am trying to do but it is generating an exception, see:

protected void onPostExecute(String resultado)
    {
        if(resultado.contains("login_erro"))
        {
            Toast.makeText(getApplicationContext(), "Login e Senha inválidos", Toast.LENGTH_LONG).show();
        }
        else
        {
            try
            {
                JSONObject json = new JSONObject(resultado);
                Usuario user = new Usuario(json.getString("login"), json.getInt("id_funcionario"), json.getInt("prior_usuario"));
                Intent abrirPrincipal = new Intent(TelaLogin.this, TelaPrincipal.class);
                abrirPrincipal.putExtra("usuario", user);
                startActivity(abrirPrincipal);
            }
            catch (Exception erro)
            {
                Toast.makeText(getApplicationContext(), erro.getMessage(), Toast.LENGTH_LONG).show();
            }


        }

    }

Exception:

Value br of type java.lang.String cannot be converted to JSONObject

This method receives the output of a post made on a page .php that is returning a json_encode with a array of the resulting table. Can you help me understand?

    
asked by anonymous 27.11.2016 / 16:01

1 answer

0
protected void onPostExecute(String resultado)
    {
        if(resultado.contains("login_erro"))
        {
            Toast.makeText(getApplicationContext(), "Login e Senha inválidos", Toast.LENGTH_LONG).show();
        }
        else
        {
            try
            {
                resultado = resultado.substring(resultado.indexOf("{"), resultado.lastIndexOf("}") + 1);
                JSONObject json = new JSONObject(resultado);
                usuarioLogado = new Usuario(json.getString("login"), json.getInt("id_funcionario"), json.getInt("prior_usuario"));
                Intent abrirPrincipal = new Intent(TelaLogin.this, TelaPrincipal.class);
                abrirPrincipal.putExtra("usuarioLogado", usuarioLogado);
                startActivity(abrirPrincipal);
            }
            catch (Exception erro)
            {
                System.out.println(erro.getMessage());
            }
        }
    }
    
27.11.2016 / 20:06