Question

I've a web page (being displayed in a browser, NOT a WebView), and I'd like to pass some data (using http POST for instance) to a regular android app.

I only need the app to be launched and fed with data. I know the app can be launched by registering an intent filter, but I'm lost on the data passing part.

There's no need for the URL to be real, a fake URL is fine.

This can be done in BlackBerry using HTTP filters. Is there a way to do the same thing in Android?

Thanks in advance.

Was it helpful?

Solution

In your web page put links like:

<a href="my.special.scheme://xyz.com/data1/data2">

In your activity's onCreate() you can access the data through the intent object..

Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "xyz.com"
List<String> params = data.getPathSegments(); 
String first = params.get(0); // "data1"
String second = params.get(1); // "data2"

OTHER TIPS

The approach I would investigate into is to implement a very simple HTTP server service as part of your application. Coding a small web server for your exact purpose would be extremely simple: Listen on TCP port 80, accept incoming connection, then keep reading from socket until \r\n\r\n is seen to skip the header, and then read the POST data. Your web page would presumably access it via 'localhost'. Whether there are any major hurdles with this method I don't know, but on doing a quick search I've seen there are already small Android web servers available, so technically it seems possible.

Here are my two cents:

Although not actually intercepting, but this approach does the job using the conventional Intent Filter mechanism.

Example:

  1. Browser opens a link to myownscheme://myfakeurl.fake/http%3A%2F%2Factualurl.com
  2. Android app matches the intent filter and is launched. It parses the parameter in the URL and decodes it obtaining "http://actualurl.com"
  3. The android app makes a connection to the passed url and fetches the data.

        String encodedURL = getIntent().getData().getPathSegments().get(0); //Returns http%3A%2F%2Factualurl.com
        String targetURL = Html.fromHtml(encodedURL); //Returns http://actualurl.com
        //Now do the GET and fetch the data.
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top