如何使用js文件获取json响应

时间:2014-12-29 13:32:33

标签: javascript android json

您好我正在开发android应用程序,因为我使用request.js使用下面的文件如何获取json数组并使用这个json数组我可以解析json对象然后我将使用服务器响应在clent。

可以帮助我。

的package.json

{
    "name": "node-chat",
    "version": "0.0.1",
    "main": "app.js",
    "dependencies": {
        "express" : "~4.0.0", 
        "mysql" : "~2.5.3", 
        "connect": "~2.14.4",
        "request": "2.36.0"


  }
}

app.js

/**
 * Module dependencies.
 */
var express  = require('express');
var connect = require('connect');
var app      = express();
var port     = process.env.PORT || 8080;

// Configuration
app.use(express.static(__dirname + '/public'));
app.use(connect.logger('dev'));
app.use(connect.json());  
app.use(connect.urlencoded());

// Routes

require('./routes/routes.js')(app);

app.listen(port);

console.log('The App runs on port ' + port);

request.js

var request = require('request');
var mysql = require('mysql');

var connection = mysql.createConnection(
    {
      host     : 'localhost',
      user     : 'root',
      password : '',
      database : 'chat',
    }
);

connection.connect();


exports.login = function(name,mobno,reg_id,callback) {

var data = {

            name    : name,
            mobno :  mobno,
            reg_id   : reg_id


        };
var que = "SELECT * from users WHERE mobno =" + mobno;

 var query = connection.query(que, function(err, rows)
        {
          if(rows.length == 0){
            var query = connection.query("INSERT INTO users set ? ",data, function(err, rows)
        {

          callback({'response':"Sucessfully Registered"});

        });
          }else {

           callback({'response':"User already Registered"});

          }

        });


}


exports.getuser = function(mobno,callback) {



 var query = connection.query("SELECT * from users", function(err, rows)
        {
          if(rows.length == 0){
            callback({'response':"No Users Registered"});
          }else {

          callback(removeUser(rows, mobno));

          }

        });

}


exports.removeuser = function(mobno,callback) {

var que = "DELETE FROM users  WHERE mobno =" + mobno;

var query = connection.query(que, function(err, rows)
        {

             if(!err){

    callback({'response':"Removed Sucessfully"});
  }else{
    callback({'response':"Error"});
  }  
        });
}



exports.send = function(fromn,fromu,to,msg,callback) {

var que = "SELECT * from users WHERE mobno =" + to;

 var query = connection.query(que, function(err, rows)
        {
          if(rows.length == 0){
            callback({'response':"Failure"});

          }else {


    var to_id = rows[0].reg_id;
    var name = rows[0].name;

request(
    { method: 'POST', 
    uri: 'https://android.googleapis.com/gcm/send',
    headers: {
        'Content-Type': 'application/json',
        'Authorization':'AIzaSyAnHnfHRvXKyIJaSjdR82QQULccWxN4nWg'
    },
    body: JSON.stringify({
  "registration_ids" : [to_id],
  "data" : {
    "msg":msg,
    "fromu":fromu,
    "name":fromn
  },
  "time_to_live": 108
})
    }
  , function (error, response, body) {

      callback({'response':"Success"});
    }
  )
}});

}

function removeUser(arr, val) {
    for(var i=0; i<arr.length; i++) {
        if(arr[i].mobno == val) {
            arr.splice(i, 1);
            return arr;
            break;
        }
    }
}

0 个答案:

没有答案