Android - 从 intent-filters 解析短 url

Android - Resolve short urls from intent-filters

短 urls 有一个奇怪的问题。

我有一个 Activity 带有 2 个不同的意图过滤器来捕捉常规 urls,但也捕捉同一网站 (Dribbble) 提供的短 urls。

这是我的清单:

    <activity
        android:name=".activities.SomeActivity">

        <!-- For "regular" urls -->
        <intent-filter>
            <data
                android:host="dribbble.com"
                android:pathPrefix="/shots/"
                android:scheme="https" />
        ...
        </intent-filter>

        <!-- For short urls -->
        <intent-filter>
            <data
                android:host="drbl.in"
                android:scheme="http" />
        ...
        </intent-filter>
    </activity>

这里有 2 个 url 匹配这些 intent-filters 的例子:

检索正常的 urls 按预期工作。但是当试图解析来自 Android 的股票浏览器的缩短的 url 时,Activity 中收到的 url 是两种类型的 url 的混合秒。即由主机+正则url的后缀组成,而短url的路径。对于前面的示例,它看起来像这样:

因此,当我尝试解析 Activity 中的短 url 时,如果 url 是一个镜头 url,我的代码不会进入条件测试]:

class SomeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        ...
        resolveUrl(getIntent().getData());
        ...
    }

    private void resolveUrl(Uri url) {
        if (url.getHost().equals("drbl.in")) { // Condition never met
            resolveShortUrl(url);
        } else {
            getIdFromUrl(url);
        }
    }

    ... 
}

有人能告诉我我到底做错了什么吗? 感谢您的帮助。

Can somebody tell me what the heck am I doing wrong?

如果你 运行 curl -v http://drbl.in/qkHY,你会得到类似于:

* Hostname was NOT found in DNS cache
*   Trying 52.6.188.250...
* Connected to drbl.in (52.6.188.250) port 80 (#0)
> GET /qkHY HTTP/1.1
> User-Agent: curl/7.38.0
> Host: drbl.in
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Mon, 12 Oct 2015 13:14:03 GMT
< Content-Type: text/html
< Content-Length: 193
< Location: https://dribbble.com/shots/qkHY
< 
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>
* Connection #0 to host drbl.in left intact

因此,Dribble 将短 URL 重定向到路径中包含短代码的 dribble.com URL。 Android 与它无关,除了是执行 HTTP 请求的人。

如果执行curl -v https://dribbble.com/shots/qkHY(redirected-toURL),运球问题另一个重定向响应,虽然一个具有 MIME 类型:

* Hostname was NOT found in DNS cache
*   Trying 52.6.188.250...
* Connected to dribbble.com (52.6.188.250) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
*    subject: businessCategory=Private Organization; 1.3.6.1.4.1.311.60.2.1.3=US; 1.3.6.1.4.1.311.60.2.1.2=Massachusetts; serialNumber=001031096; street=Ste. 202; street=16 Front St.; postalCode=01970; C=US; ST=Massachusetts; L=Salem; O=Dribbble LLC; CN=dribbble.com
*    start date: 2014-12-31 00:00:00 GMT
*    expire date: 2016-04-29 12:00:00 GMT
*    subjectAltName: dribbble.com matched
*    issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 Extended Validation Server CA
*    SSL certificate verify ok.
> GET /shots/qkHY HTTP/1.1
> User-Agent: curl/7.38.0
> Host: dribbble.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Mon, 12 Oct 2015 13:16:25 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Status: 301 Moved Permanently
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Location: https://dribbble.com/shots/2278534-Google-Now-Animation
< Cache-Control: no-cache
< Set-Cookie: _dribbble_session=eGhSVTlDV1NEMzBiTUlLMTA0YWlkQ0YzWUJiYUM2cGVGQ1J1cFZYTE9xL3R3UEsvb0VWS1paU1U3bkNRdlh1V3JJZ2JqdW1veTM0azMrTVFRUUlMYUE9PS0tdC9uQTlNR0xvKy9GQmx0R3BkTUtWQT09--9873ba3016e4ab9a99f6a237a975610c01c1eb15; path=/; secure; HttpOnly
< X-Request-Id: 5359c3d6-9d64-4637-8ff5-014e8e44d503
< X-Runtime: 0.006814
< Strict-Transport-Security: max-age=31536000
< X-Frame-Options: SAMEORIGIN
< 
* Connection #0 to host dribbble.com left intact
<html><body>You are being <a href="https://dribbble.com/shots/2278534-Google-Now-Animation">redirected</a>.</body></html>

一个 HTTP HEAD 请求(将 -X HEAD 添加到 curl 命令)returns 相同的基本内容,减去body.

我最好的猜测是 Android 在第二个 HEAD 请求中看到 Content-type header 并继续将 URL 用于 Intent 分辨率。您对此无能为力,因为我认为您既无法控制 Android 也无法控制 Dribble。但是,您始终可以通过在 Java 代码中发出自己的 HEAD 请求来获得完整的 URL。