Android Tutorial Embed an Http Server in your Android Application

Status
Not open for further replies.

Ekcentrik

Member
Licensed User

KY Leng

Member
Licensed User
If you know the device ip address then you can connect to it with any valid connection. What I meant to say without a more complex solution it is not possible to access the server over the internet.
Good evening,

I know that this HttpServer library could not be accessed over internet.
However, If I want to convert my Android device to be a Webserver and can be accessed over internet, what should I do ? Do I need to have a public IP address for my device?

Looking forward to hearing from you soon?
 

DavideV

Active Member
Licensed User
i've done it using duckdns service: http://www.duckdns.org/about.jsp
this service does't expire monthly like no-ip does and it's completely free.
The router configuration could be the tricky part.
I've a full working b4a app (not yet published) based on both no-ip and duckdns that can handle your own webserver at home (example in my signature). i could share some pieces of code if someone need (not the entire app since i'm going to publish it)).

cheers
 

DavideV

Active Member
Licensed User
Hi Erel

it's possible to upload file to web server (android device 1) from job.postfile (android device 2).
The donwload job works fine with job1.download("http://XX.XX.XX.XX:5555/download/mnt/sdcard/namefile")
I have tried job1.postfile("http://XX.XX.XX.XX:5555/upload/mnt/sdcard/",File.DirInternalCache,"datastore") but I received SERVER ERROR.

Thanks.
Hi,
i was able to upload files from an android app (device1) to an android webserver at home (device2) using this:
http://www.b4x.com/android/forum/threads/android-http-multipart-requests.8411/#content

Bye
 

opus

Member
Licensed User
Hi,
could anybody show how this WebServer could be able to read the credentials that are sent to the server by another app using:
B4X:
HTTPClient.ExecuteCredentials( job.GetRequest, taskCounter, job.Username, job.Password)
The other app is using the httpUtils2-lib within the AppUpdating-Lib.
It is working without the credentials (connection over internet and all that...).
I can't find a way the see any part of the credentials that are set and send(at least I think it is sending them:().

Thanks
 

DonManfred

Expert
Licensed User
I can't find a way the see any part of the credentials that are set
Credentials which are set are not used if the Server does not request them. In a normal webserver you need to use a .htaccess file. I dont know if this lib supports this.

But you could use random Headers which you send with the request.

B4X:
    Dim job As HttpJob
    job.Initialize("getuser",Me)
    job.Download("http://192.168.192.24:5555/") 'http://donmanfred.basic4android.de/req.php
    job.GetRequest.SetHeader("Username", "MyUsername")
    job.GetRequest.SetHeader("Password", "mypass")
and in server app

B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    Try
        Log("Request: Username="&Request.GetHeader("Username"))
        Log("Request: Password="&Request.GetHeader("Password"))
        Log("Client: " & Request.RemoteAddress)
        Log(Request.RequestURI) 'handle the request based on the URL
 

opus

Member
Licensed User
Credentials which are set are not used if the Server does not request them. In a normal webserver you need to use a .htaccess file. I dont know if this lib supports this.
I guess that is the reason I couldn't get anything.

On the server-side I already used your posted Log-Lines, however no result so far.

Which lib is this one from?
B4X:
job.GetRequest.SetHeader("Username", "MyUsername")
The IDE tells me its not in httpUtils2.


Thanks
 

Jean Pierre MALOBERTI

Member
Licensed User
Hi,

I'm testing B4A for two days and i'm trying to run the httpserver examples (HttpServer and HttpServerDB). The compilation is ok and the app starts on the device (or the emulator) but the command "server.Start(port)" throw the following error :

java.lang.RuntimeException: Unable to create service b4a.example.serverservice: java.lang.RuntimeException: java.lang.ExceptionInInitializerError
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3272)
at android.app.ActivityThread.access$1900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.RuntimeException: java.lang.ExceptionInInitializerError
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:165)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at b4a.example.serverservice.onCreate(serverservice.java:53)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3262)
... 9 more
Caused by: java.lang.ExceptionInInitializerError
at anywheresoftware.b4a.objects.HttpServer.Start(HttpServer.java:54)
at b4a.example.serverservice._service_create(serverservice.java:230)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
... 12 more
Caused by: java.util.MissingResourceException: Can't find resource for bundle 'javax.servlet.LocalStrings_fr_FR', key ''
at java.util.ResourceBundle.missingResourceException(ResourceBundle.java:238)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:230)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:139)
at javax.servlet.GenericServlet.<clinit>(GenericServlet.java:65)
... 22 more

Any idea ?

Thanks
 

Erel

Administrator
Staff member
Licensed User
I tried to switch to France locale, however it worked fine here. So I'm not sure whether the following code will solve this issue or not:
B4X:
Dim jo As JavaObject
jo.InitializeStatic("java.util.Locale")
jo.RunMethod("setDefault", Array(jo.GetField("US")))
Add it before you initialize the server object.
 

Jean Pierre MALOBERTI

Member
Licensed User
Thanks for the quick reply but now the error is :

Caused by: java.util.MissingResourceException: Can't find resource for bundle 'javax.servlet.LocalStrings_en_US', key ''
at java.util.ResourceBundle.missingResourceException(ResourceBundle.java:238)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:230)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:139)
at javax.servlet.GenericServlet.<clinit>(GenericServlet.java:65)
... 22 more
 

Erel

Administrator
Staff member
Licensed User
The problem is related to a limitation of the trial version. It doesn't support libraries with internal resource files such as this one.
 
Status
Not open for further replies.
Top