Serve static files over http within an android app with WebView - android

I have a nuxt.js 3 generated javascript app, which is just some html, JS & other assets. My issue is that passing a file://<path>/index.html as a url to a web_view means that assets are broken since all addresses are origin-relative, e.g. /home/index.html, /_nuxt/entry.123456.css, not path-relative like home/index.html. (i think it would not work for subpages anyway, being path-relative)
How to overcome this?
is there any way to set the web_view to know where to get the static files from? there are no other requests from the web_view other than GETs for static assets & changes of location. I'm thinking something like request interception and rewriting the path or even the response. but that sounds just like a server, so:
I guess I'm gonna need a http server. Can you suggest me some simple up-to-date library that can serve files within an android app? Or can you point me to some API from android native toolbox to open a socket and serve static files via http response? Also, will such a server be accessible for other apps, for the whole android device on localhost? I would like to avert that; the only consumer of the files should be the web_view.
To avoid obvious counter-question - but why? It's that I intend to build a hybrid android app, that does simply open a web_view full-screen. I am aware of limitations of such solution and I'm fine with that. The android part is where I'm not well experienced. If this gets too over-complicated, I'm gonna just change the way how to produce the js app in the first place. but nuxt.js is my best option for now.

Related

Converting html5 to offline android app

So here's my problem. I made a website, which by popular demand, people want it to be an app on there phone (because apparently people are to lazy to google it and use precious internet :3 )
So I did some research and came across the android webview, and after a few tutorials I got my website working as an app. However this app still needed internet.
So I tried to just put my website files (Which are inside a folder called version) inside the android res folder
WebView/app/src/main/res/
But when I try to access it with the following code
String url = "/res/version/english.html";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
It comes up with the "Webpage not available" message.
Does anyone know how I can fix this. Thanks
My suggestion would be to look into Cardova or PhoneGap. They provide extremely robust tools to program with HTML, CSS, Javascript, etc and then acts as a native bridge between Java or Objective-C if you're developing for iPhone.
You will still have access to many of the core native API's such as Camera, Accelerometer, etc. My company uses these religiously as we mostly have web developers on staff. I would highly suggest it.
I figured out what I did wrong. First off, I needed to add this at the start of it
"file:///"
Then I needed to make an assets folder, and then put all my files into it. And the last mistake, I need to access the access folder by doing this
"file://android_assest/*"

Cordova - Can static HTML/JS/CSS be encrypted in the distributable device specific files

When I built an APK file using cordova build, it bundles all the HTML/JS/CSS3 static files into the APK file in simple plain/text format. This way anyone will be able to easily use this code and create copy of my app. Is there any way to encrypt these files?
There is no 100% foolproof way for this, I could think of a solution where you will need a web server running a script for authenticating and then providing the core Javascript or a URL for the same in response. And you could save it locally for future use.
To keep the access restricted to your application, you will have to get the app's package name and then only provide the response. This post should help you to get your package's name.
In addition, you could add some logic on your server as well as your Javascript where you will generate a random unique string based on the timespan and add it to authentication process' parameters. And obfuscating all of your packaged JS in the apk will make it difficult for anyone to get your whole logic.

Downloading fails much more frequently on iOS than on Android?

In my iOS project, I use NSURLConnection to download files. My code is just like the http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html document.
In my team's Android project, we use Android's API to download the same files. Nothing special either.
Both the iOS and Android project have the same fail-retry mechanism, and both of them send the same statistic data to server.
The files they trying to download is between 1M to 10M.
The statistic server shows that for the same URL, download failure is 2% on android, but 20% on iOS! Since they are trying to download the same URL, it seems not the server's problem but more like a client issue.
Why iOS download would fail so frequently? Is there any special APIs I should use for iOS, to make the download robust? Currently I'm using NSURLConnection, and I've just found NSURLDownloader which is more convenient to write files. Will they be different for download success percentage?
P.S. the error I got by -
(void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) , is usually -1005, NSURLErrorNetworkConnectionLost.
I'd suggest looking more into why you're getting NSURLErrorNetworkConnectionLost. Even if your device says it's connected, sometimes it could lose the connection and be attempting to regain it before the indicators update accordingly.
This is a good starting point for more information on better dealing with reachability. Checking For Internet Connectivity in Objective C
Otherwise, I recommend you post your download-related code so others can look for possible issues.

path problems porting web-app to phonegap

I have what I think is a fairly standard set-up of an existing web-app and would like advice on how best to adapt it for creating native versions via PhoneGap, in a way that we can keep developing the web-app and updating the phonegap generated versions from it with minimal rework.
I am a PhoneGap novice. I have searched around and tried various suggestions from StackOverflow etc with no luck, at least for my set-up.
The app is developed in GWT and consists of:
static resources in a shared folder for all of the app
/static/ with images, fonts, css.
The css define some font-families, referencing font files in /fonts
We refer to these static resources from html files and JavaScript code using absolute
paths.
static resources in a number of sub-folders (by GWT modules FYI)
Such as /LoginGadget, which will have GWT generated html, Javascript and sometimes subfolders with css and css-images.
GWT-RPCs
These are basically servlets where GWT takes care of serialization etc, and are accessed from our client code via XHRs under the covers
PhoneGap Build
I have started looking at creating a packaged app using PhoneGap and run into some problems where I need advice (Android example).
I have created /assets/www and put an index.html file in there and got it going.
I copied a version of our /static/ folder and our /LoginGadget folder under that "root" to see, and started it using
super.loadUrl("file:///android_asset/www/index.html");
which works.
In all our GWT generated html and Javascript files we have references to static resources using both absolute (e.g. "/static/....") and relative paths (e.g. from a LoginGadget Javascript, it might reference "css/some.css").
The relative paths work, as they reside "under" the folder where the html/js that references them lie.
Problem 1
However, references to absolute paths fail, despite PhoneGap starting by saying:
DroidGap: url=file:///android_asset/www/index.html baseUrl=file:///android_asset/www/
I expected a reference to "/static/images/file.png" from say index.html to be appended to the "baseUrl" to give file:///android_asset/www/static/images/file.png and hence work, as that is where the file lies.
I have had to modify index.html to use "static/image/file.png" for it to work.
But I'd have to recompile all our GWT app with a different configuration to modify all references to resources, and references from other files in sub-folders back to "/static/" won't work if modified to be just "static/".
How can I get absolute path references to "map" to /assets/www or similar?
(See below, I've looked at using the "base" tag....)
Problem 2
The GWT RPCs make the XHR request to the server the html/js was served from.
This works great as the app doesn't have the host server name hard-coded, and is in fact deployed on many different appengine appids/domains for testing etc.
Here the html/js files are "served" from file:/// hence I need to specify the server somehow.
I tried specifying with the "base" tag as documented, but then any reference I have in my html/js to a resource that doesn't specify "file://" seems to be made to the server specified in "base"..... hence I am not loading my local resources anymore and I basically have a web-app served from my server.
Want
What I'd like to do, is to be able to take the (pretty large) compiled and tested app from my wab-app's war (/static and all my /GWT-Module folders) untouched and copy them into /assets/www on my PhoneGap app and then add some boiler plate or start-up code and run as is.
Sounds like a big request, but I think if I could properly specify two things:
file path to use as "root" for absolute paths for resource requests that don't specify a http/https protocol (or other protocols.....which I already see are handled in DroidGap.java)
server (protocol, hostname, port) to use for any XHR requests
Then everything would come out in the wash!
I thought this would be a faily "standard" setup and already covered. Maybe it is and I'm just missing something.
Comments? suggestions?
Thanks in advance for any help.
Do you know about mgwt? http://www.m-gwt.com
Its a mobile framework for GWT built by one of the GWT Steering Committee members and it has phonegap integration for GWT apps as well.
There is a lot of docs in the projects and a very friendly user group.
Let me address your problems with a little more detail:
Problem1:
If you are using absolute path you will get that inconsistent behavior with phonegap, since the browser builds those urls locally and a reference to / means the root entry on the phone.
Using absolute URLs is a bad idea most of the times and I would recommend you to change that in your app (as you already did).
Problem2:
To use GWT RPC with phonegap read this blog post that I made to address the issues: http://blog.daniel-kurka.de/2012/04/gwt-rpc-with-phonegap-revisited.html
We solved these problems with two separate actions:
1) I wrote an ant target that as part of our build takes the web-app, and replaces the paths in all internal url references it finds, making them fit what's needed for them to work within the app. It still seems a major bug in phonegap to me. Maybe it's been fixed in newer versions?
2) We subclassed the RPC service class and modified it to have a static member that is the service endpoint. At the start-up of our phonegap app we modify the end-point to point to the desired server and presto.... all our GWT RPCs point to the right place.

Configuration file in Android. Does that exist?

I have some configuration I want to save it in my Android application and read it whenever I need , for instance, the server URL that it should try to access like that.
Is there any similar mechanism like web.config in ASP.NET available in Android?
A central configuration file that can be set up manually and then read by the application? Any help would be appreciated!
We use a .properties file in assets folder. It works out very well for us as we support multiple carriers with this, write to it (in case some values, sent from server, need to change. This is done at app start time, thus making our code configurable from server).
You can throw things like that into your strings.xml file. But, since you can't actually modify these values in real-time (since it's a distributed application rather than running on a server), throwing it into a constants class is quite acceptable.
Use Shared Preferences.
Here's a link Shared Preferences
You can use sq lite database files for it. You have a native API to read and write those and on top of that a command line tool.
If you want to create an XML file instead, then it's no different than any other xml file (unless you are thinking about the Shared Preferences, which use an xml format to save the data, but I believe it's not the best API for your application).
I was stumped on this too, but came across Managed Configurations in the Android documentation.
Managed configurations, previously known as application restrictions, allow the enterprise administrator to remotely specify settings for apps. This capability is particularly useful for enterprise-approved apps deployed to a managed profile.
It allows you to set a default value in case you rather not getting into the enterprise admistration business but leaves that option open for the future.
There is a caveat. This only works if your app is registered for EMM. Otherwise you will retrieve an empty map of restrictions.

Categories

Resources