简体   繁体   中英

Get separate JSON from the response

My team & I are working on a chatting app: an Android client, & a web client.
Recently, we stumbled up a blocking issue. Here is how:

So the web & the Android client (I will refer to them as 'the clients' from now on) communicate with a Node.js server.

We're actually working on the login/signup section. This is an example of what we have so far:

Android client

  • MainActivity.java

     Button loginButton = (Button) findViewById(R.id.loginButton); loginButton.setOnClickListener(new LoginListener()); private class LoginListener implements View.OnClickListener { @Override public void onClick(View v) { CustomAsyncTask manageSession = new CustomAsyncTask(); Map<String, String> params = new HashMap<>(); params.put("postURL", "/signin"); params.put("username", mUsername.getText().toString()); params.put("password", mPassword.getText().toString()); manageSession.execute(params); } } 

Briefly, what this code does, is that it sends a parametrized POST request to the /signin route.

Web client

<form ...>...</form>

<script type="text/javascript">
    $('button').click(function(){
        $.post('/signin',{username : $('#username').val(),password :$('#password').val()});
    })
</script>

Server side

// signin post
app.post('/signin', function (req, res) {
    connection.query('SELECT * FROM User WHERE ((username = "' + req.body.username + '" OR email = "' + req.body.username + '") AND password = "' + req.body.password + '")', function (err, rows) {
        if (err) throw err;
        // If the user entered a valid username or email, then check for the password
        if (rows.length > 0 && rows[0].password == rows[0].password) {
            res.render('index', { status: "SUCCESS", username: req.body.username })
        } else {
            // Valid username or email, but invalid password
            res.render('index', { status: "FAILURE", username: req.body.username })
        }
    });
})

The problem

As you can see from the code snippets above, the clients send POST requests to the /signin route, but:

  • The Android client expects pure JSON as a response
  • The web client expects a whole page as a response (index.ejs)

res.render() solves the problem for client 2 , res.end({json}) solves it for client 1 .

Is there a way we could separate the response, so that each client gets what it wants ?
What is the optimal way to work this out ?

You can add an extra variable to get if it is coming from web or android and then do as needed. eg-

app.post('/signin', function (req, res) {
    connection.query('SELECT * FROM User WHERE ((username = "' + req.body.username + '" OR email = "' + req.body.username + '") AND password = "' + req.body.password + '")', function (err, rows) {
        if (err) throw err;
        // If the user entered a valid username or email, then check for the password
        if (rows.length > 0 && rows[0].password == rows[0].password) {
            if(req.body.device_type == "web"){
                res.render('index', { status: "SUCCESS", username: req.body.username })
            }
            else{
                res.end({ status: "SUCCESS", username: req.body.username })
            }
        } else {
            // Valid username or email, but invalid password
            if(req.body.device_type == "web"){
                res.render('index', { status: "FAILURE", username: req.body.username })
            }
            else{
                res.end({ status: "FAILURE", username: req.body.username })
            }
        }
    });
})

and in android:-

params.put("postURL", "/signin");
params.put("username", mUsername.getText().toString());
params.put("password", mPassword.getText().toString());
params.put("device_type", "android");
manageSession.execute(params);

in web:-

$.post('/signin',{username : $('#username').val(),password :$('#password').val(),device_type:'web'});

Let me know if there's any more problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM