5

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.

Ron
  • 24,175
  • 8
  • 56
  • 97
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • 1
    There are some SO Questions http://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser , http://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app and http://stackoverflow.com/questions/3038958/launch-app-with-url – user370305 May 15 '12 at 09:53
  • @user370305 Thanks, I've read them all. But, as I tried to explain in my question, I already know how to launch the app but not how to pass data. – Mister Smith May 15 '12 at 10:10
  • 1
    check my blogspot `html and activity links` It contains info about passing data (using GET): http://sherifandroid.blogspot.com/2011/09/html-and-activity-links-in-textview.html – Sherif elKhatib May 18 '12 at 11:00
  • @Sherif +1 for showing how to use `getQueryParameter`. – Mister Smith May 21 '12 at 07:18

3 Answers3

4

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"
Ron
  • 24,175
  • 8
  • 56
  • 97
2

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.

Trevor
  • 10,903
  • 5
  • 61
  • 84
1

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.
    
Mister Smith
  • 27,417
  • 21
  • 110
  • 193