0

I am planning to make a local server in computer. Any Android device that connected to this local server can request data from the computer.

However, I am lack of concept how to realize it. For the local server, I planned to use C# to do it but is it just make a SQL database? If yes, then how the Android apps can request data from local server? For the Android app, what is the API used to request the data? I have some experience on both C# and Android.

I actually planned to do this in cross-platform using Xamarin but I have doubts. For example, if I want to do apps for Android and IOS, then I only code once and can be used in Android and IOS? If yes, how about the UI design?

I know that asking this question is too general and supposing not to ask here but I really cant find the answer online. I just hope that anyone can briefly tell me the steps/tools to realize it or some important terms/concepts so I can further study on that. Thanks a lot.

Sam
  • 1,252
  • 5
  • 20
  • 43

2 Answers2

2

Using C# to host your RESTFUL service is certainly feasible, but sometimes overkilling.

You might use node js to setup everything in a minute or two:

//Server.js
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080; 
//We need a function which handles requests and send response
function handleRequest(request, response){
    response.end('It Works!! Path Hit: ' + request.url);
}

//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});

Now run this JavaScript to start your web service:

> node Server.js

Then try access this service in your browser to see the result! That is it!

Note that once you defined such web service, no matter you are requesting it via Android or browser, you get the same response.

Community
  • 1
  • 1
David
  • 15,894
  • 22
  • 55
  • 66
2

You can do it in form of a Http Server which an android device can connect to using URL Connection in C# you can you TcpListener for the server and use StreamReader to read request and StreamWriter to reply. If you are doing this in java then you can use Printwriter for the server which is a lot easier since java is part of android.
C# Server code

using System;
using System.IO;
using System.Net.Sockets;
using System.Web;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {

            TcpListener listener = new TcpListener(1302);
            listener.Start();
            while (true)
            {
                Console.WriteLine("Waiting for connection");
                TcpClient client = listener.AcceptTcpClient();
                StreamReader sr = new StreamReader(client.GetStream());
                StreamWriter sw = new StreamWriter(client.GetStream());
                try
                {
                    //client request
                    string request = sr.ReadLine();
                    Console.WriteLine(request);
                    string[] tokens = request.Split(' ');

                    string reply= tokens[1];
                    if (reply == "/")
                    {
                        reply = "Welcome";
                    }
                    //Handle request


                    sw.WriteLine("HTTP/1.0 200 OK\n");
                    string data = "You can handle request this way";
                        sw.WriteLine(data);
                        sw.Flush();



                }catch(Exception ex)
                {
                    //error\
                    sw.WriteLine("HTTP/1.0 404 OK\n");
                    sw.WriteLine("ERROR");
                }
                client.Close();
            }
        }
    }
}

EDIT: So if you want to trying to connect the url is your server ip and port in this case is used 1302 so if your ip is 127.0.0.1 your url is http://127.0.0.1:1302/ Note that it can also be accessed from your android browser and you C# server should be granted firewall permission.

Collins
  • 145
  • 2
  • 15