Read JSON file on android

0

I try to log-in, the server being programmed in Javascript (using node.js). The problem that I have (which I saw is very common but still do not find my problem back) is that when I try to read it I get the following message: Value //Objeto JSON// of type java.lang.String cannot be converted to JSONObject . The server code is as follows:

app.post('/Acceso', function(req, res){
    var conDB=DB();
    var UsuarioReg=req.body.Usuario;
    var ContraReg=req.body.Contra;
    conDB.query('SELECT * FROM Usuario WHERE Usuario = ? and Contra= ?',
        [UsuarioReg,ContraReg],
        function(err,rows){
            conDB.end();
        if(err)
        {
            return console.log(err);
        }
        else
        {
            if (!rows.length)
            {
                var obj='{"log":"no"}';
                return res.json(JSON.stringify(obj));
            }
            else
            {
                console.log(rows);
                var obj='{"log":"si"}';
                return res.json(JSON.stringify(obj));
            }
        }
    });
});

The user code that is responsible for reading what it says in log is this:

import org.json.JSONException;
import org.json.JSONObject;

    public class LeerConsulta{

        JSONObject jsonResponse;
        String content="";

        public LeerConsulta(String data)
        {
            content=data;
        }
        protected String onPostExecute()
        {
            try
            {
                jsonResponse = new JSONObject(content);
                String log=(String)jsonResponse.get("log");
                return log;
            }
            catch(JSONException e)
            {
                String error=e.getMessage();
                return error;
            }
        }

    }

I know that it can be as bad as I write the string that is transformed into JSON when I send it to the user, as well as how I take its content. That's why I send both. EDITION: the main code that is responsible for making the relationship with the class that reads the query in JSON is as follows:

aceptar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usuario=Usuario.getText().toString();
                String contra=Contra.getText().toString();
                Ajax logIn=new Ajax(new String[]{
                        "Usuario="+usuario,
                        "Contra="+contra
                });
                try {
                    String acceso = logIn.execute(IP_ADDRESS+"Acceso").get();
                    LeerConsulta lec=new LeerConsulta(acceso);
                    String conf=lec.onPostExecute();
                    Toast.makeText(MainActivity.this, conf, Toast.LENGTH_LONG).show();
                   /* if(!acceso.equals("si"))
                        Toast.makeText(MainActivity.this, "Usuario y/o clave no valida", Toast.LENGTH_SHORT).show();
                    else
                    {
                        Intent intent=new Intent(MainActivity.this, Accedido.class);
                        startActivity(intent);
                    }*/
                } catch(Exception exc){
                    Toast.makeText(MainActivity.this, exc.toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    
asked by F. Riggio 08.01.2017 в 20:32
source

1 answer

0

The error that comes to you says everything.

  

Value // JSON object // of type java.lang.String can not be converted to   JSONObject

Replace in your Java:

jsonResponse = new JSONObject(content);

By:

jsonResponse = new JSONObject(content);
String log = (String) jsonResponse.get("log");
System.out.println("log: " + log);

What your NodeJS server sends is fine. In your Java just the problem is.

    
answered by 08.01.2017 / 21:12
source