1

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 || 1232);

//
//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 collagenames', 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,

  • This is too broad .. Where are you stuck? Seems like you want others to write code for you as written. – WiredPrairie Aug 27 '13 at 11:19
  • I have written that code .... sorry ... i am a newbie so i not good at explaining technically ...... im trying to find out how to send input to server from android .......... and what changes according to that should i need to make on my express code .. –  Aug 27 '13 at 11:30
  • 1
    It doesn't feel like you've looked around. First google hit: http://stackoverflow.com/questions/6047194/how-to-call-restful-web-service-from-android – WiredPrairie Aug 27 '13 at 12:02

0 Answers0