Web Service that runs on Android Device

realblue

Member
Licensed User
Longtime User
Hi everybody,

I need to write a web service that will run on Android Device.

I have an Android Device that collects some data thru a serial device and stores in an internal database. I need to access this data by a web service from several consumers (like another android device or from any browser etc).

I know how to develop a web service under Windows OS with DotNet. How can I develop a web service under Android?

Any suggestion? :sign0163:
 

realblue

Member
Licensed User
Longtime User
I think I will try to implement TCP/IP socket (in Windows WinSock) approach.

I am planning to write a TCP/IP listener in Android. Which library should I use?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Look at the source code of the B4A WebServer that i posted previously.
It should show you exactly what you can implement on the B4A device to listen for web service requests (i think).

Martin.
 
Upvote 0

realblue

Member
Licensed User
Longtime User
Thank you "warwound" the example you have given solved my problem.

I took the HTTP example and change "AStreams_NewData" procedure as follows :

Sub AStreams_NewData (Buffer() As Byte)
Dim Bconv As ByteConverter
Dim msg As String
Dim FN As Filepath
'Log("buffer=" & BytesToString( Buffer, 0, Buffer.Length, "UTF8"))
Dim command() As String
command = Regex.Split(" ",BytesToString( Buffer, 0, Buffer.Length, "UTF8"))
If command(0) = "GET" Then
Select Case command(1).ToLowerCase
Case "/favicon.ico"
msg = HTTP.HandleGetRequest(Bconv.StringToBytes("GET /favicon.ico HTTP/1.1", "UTF8"), AStreams)
Case "/command=1"
msg = HTTP.HandleGetRequest(Bconv.StringToBytes("GET /1.XML HTTP/1.1", "UTF8"), AStreams)
Case "/command=2"
msg = HTTP.HandleGetRequest(Bconv.StringToBytes("GET /2.XML HTTP/1.1", "UTF8"), AStreams)
Case Else
msg = HTTP.HandleGetRequest(Bconv.StringToBytes("GET /ERROR.XML HTTP/1.1", "UTF8"), AStreams)
End Select
Else
msg = HTTP.HandleGetRequest(Bconv.StringToBytes("GET /ERROR.XML HTTP/1.1", "UTF8"), AStreams)
End If
FN = HTTP.MapDirectory(msg)
End Sub


Now when android device runs this little http server code, with any browser with in the same network you can issue a command like "http:<android_server_ip>:<default_port>/command=1" (eg http://192.168.1.51:8080/command=1). The above code interprets command number returns the data as an XML file.

I think Case "/favicon.ico" is necessary otherwise browser hangs.

Thank you everyone, and hope that this quick sample gives you new ideas.
 
Upvote 0
Top