Monday, 26 August 2013

ExpressJS on server and android as client

ExpressJS on server and android as client

I am using ExpressJS on server & Android as client
I want to know how to perform this action in server::
suppose i have a listView of list of colleges, I get the data for this as
JSON using the Express program shown below.
Now Android has OnClick for identifying the position on the listview
How to get the JSON for the android client dynamically ( If i click on a
row, I want to do the database query dynamically on the server so that
JSON URL is dynamically generated )
I have not posted android code as I think modification is on server side
but android developers who worked with ExpressJS or NodeJS will also have
an idea for this



What code changes should i need to make on server ?
What concepts should i need to look to achieve this functionality?



My Express program on server ::
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: 'xxx',
password: "xxx",
database: 'collages'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 7002);
//
//REQUEST FOR FIRST REQUEST
//
app.get('/',function(request,response){
var name_of_Collages, RestaurantTimings;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM restaurants', function(err,
rows, fields)
{
console.log('Connection result error '+err);
name_of_Collages = rows;
callback();
});
},
// Get the second table contents
function ( callback ) {
connection.query('SELECT * FROM RestaurantTimings', function(err,
rows, fields)
{
console.log('Connection result error '+err);
RestaurantTimings = rows;
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'Collages' : name_of_Collages,
'RestaurantTimings' : RestaurantTimings
});
} );
} );
//
//REQUEST FOR Collage SCREEN
//
app.get('/College1',function(request,response){
var College1, RestaurantTimings;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM College1', function(err, rows,
fields)
{
console.log('Connection result error '+err);
College1 = rows;
callback();
});
},
// Get the second table contents
function ( callback ) {
connection.query('SELECT * FROM RestaurantTimings', function(err,
rows, fields)
{
console.log('Connection result error '+err);
RestaurantTimings = rows;
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'College' : College1,
'RestaurantTimings' : RestaurantTimings
});
} );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Hope i am clear
Thanks,

No comments:

Post a Comment