5

我有一个网页(显示在浏览器中,而不是 WebView),我想将一些数据(例如使用 http POST)传递给常规的 android 应用程序。

我只需要启动应用程序并提供数据。我知道可以通过注册意图过滤器来启动该应用程序,但我在数据传递部分迷失了方向。

URL 不需要是真实的,假 URL 就可以了。

这可以在 BlackBerry 中使用 HTTP 过滤器完成。有没有办法在 Android 中做同样的事情?

提前致谢。

4

3 回答 3

4

在您的网页中放置如下链接:

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

在您的活动中onCreate(),您可以通过意图对象访问数据。

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"
于 2012-05-18T13:54:07.243 回答
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.

于 2012-05-18T07:47:35.043 回答
1

这是我的两分钱:

虽然实际上不是拦截,但这种方法使用传统的 Intent Filter 机制完成了这项工作。

例子:

  1. 浏览器打开指向 myownscheme://myfakeurl.fake/http%3A%2F%2Factualurl.com 的链接
  2. Android 应用程序匹配意图过滤器并启动。它解析 URL 中的参数并对其进行解码,从而获得“http://actualurl.com”
  3. android 应用程序与传递的 url 建立连接并获取数据。

        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.
    
于 2012-05-18T09:01:04.137 回答