-1

短网址有一个奇怪的问题。

我有一个带有 2 个不同意图过滤器的 Activity 来捕获常规 url,以及同一网站 (Dribbble) 提供的短 url。

这是我的清单:

    <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 示例:

检索正常 url 按预期工作。但是,当尝试解析来自 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);
        }
    }

    ... 
}

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

4

1 回答 1

2

有人能告诉我我到底做错了什么吗?

如果你运行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(重定向到的 URL),Dribble 会发出另一个重定向响应,虽然是一个 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>

HTTPHEAD请求(添加-X HEADcurl命令)返回相同的基本内容,减去正文。

我最好的猜测是,Android 会看到Content-type第二个HEAD请求的标头并继续使用该 URL 进行Intent解析。您对此无能为力,因为我认为您既不能控制 Android 也不能控制 Dribble。HEAD但是,您始终可以通过在 Java 代码中发出自己的请求来自己获取完整的 URL 。

于 2015-10-12T13:22:18.810 回答