【问题标题】:Android deep linking web url format errorAndroid 深度链接网址格式错误
【发布时间】:2016-02-22 09:29:18
【问题描述】:

我已将深度链接集成到我的文章应用中,并将此代码添加到我的文章详情网页中。

<link rel="alternate"
          href="android-app://com.example.android/myapp" />

当我在谷歌上搜索一篇文章并点击一篇文章时,它会转到我的应用程序

String action = intent.getAction();
String data = intent.getDataString();

但这里的数据变量是正常链接(www.myapp.com/...),而不是格式化(android-app://....)

为什么它像普通链接一样,但没有按照深度链接格式格式化?

我的清单文件中有意图过滤器:

<activity
            android:name="MyActivity"
            android:configChanges="orientation|screenSize"
            android:launchMode="singleTop">


     <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data android:scheme="http"
                        android:host="www.myapp.com"
                        android:pathPrefix="" />
     </intent-filter>

      <intent-filter>
                  <action android:name="android.intent.action.VIEW" />
                  <category android:name="android.intent.category.DEFAULT" />
                  <category android:name="android.intent.category.BROWSABLE" /> 
                  <data android:scheme="http"
                      android:host="myapp"
                      android:pathPrefix="" />
              </intent-filter>

     </activity>

【问题讨论】:

  • ... 因为您发送的链接是 android-app://com.example.android/myapp 而不是 www.myapp.com ?或者等等,更好的问题 - 你能告诉我们你来自 AndroidManifest.xml 的IntentFilter 吗?
  • @Shark 我在清单文件中添加了意图
  • 尝试添加pathPattern 再试一次,乍一看还可以。

标签: android web deep-linking


【解决方案1】:

这可能是由于您在 AndroidManifest 中为您的活动定义的意图过滤器,请分享如下所示的部分:

        <activity
        android:name=".ActivitySplash"
        android:configChanges="orientation|screenSize|keyboardHidden"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" android:host="www.mywebpage.com" android:pathPattern=".*" />
            <data android:scheme="https" android:host="www.mywebpage.com" android:pathPattern=".*" />
            <data android:scheme="http" android:host="127.0.0.1" android:pathPattern=".*" />
            <data android:scheme="https" android:host="127.0.0.1" android:pathPattern=".*" />
            <data android:scheme="http" android:host="my.webpage.com" android:pathPattern=".*" />
            <data android:scheme="https" android:host="my.webpage.com" android:pathPattern=".*" />
        </intent-filter>
    </activity>

然后我从点击的链接中获取我的东西,如下所示:

        // check if this intent is started via custom scheme link
    if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
        Log.d("DEEPLINK", "Deep link data detected, initializing in deep link mode...");
        Uri uri = intent.getData();
        String origin = intent.getDataString();
        //Uri uri2 = Uri.parse(Uri.encode(origin));
        String actionString = uri.getQueryParameter("action"); // get 'action'

        //fetch possible NUM variations: nmbr, num, arg
        String pno_alias1 = uri.getQueryParameter("nmbr"); // get 'pno'
        String pno_alias2 = uri.getQueryParameter("num"); // get 'pno'
        String pno_alias3 = uri.getQueryParameter("arg"); // get 'pno'

        String pno = (pno_alias1 == null) ? ( (pno_alias2 == null) ? (pno_alias3 == null ? null : pno_alias3) : pno_alias2 ) : pno_alias1;

        importantNumber = pno;
        //fetch possible zip variations: plz, zip
        String zip_alias1 = uri.getQueryParameter("zip"); // get 'zip'
        String zip_alias2 = uri.getQueryParameter("plz"); // get 'plz'
        String zip = (zip_alias1 == null) ? ( (zip_alias2 == null) ? null : zip_alias2 ) : zip_alias1;

        Toast.makeText(this, "Intent URI: " + uri + "\n\nAction: " + actionString + "\nPNO: " + pno + "\nZip: " + zip, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Parameter names: " + uri.getQueryParameterNames() + "\n\nOrigin string: " + origin, Toast.LENGTH_LONG).show();

        if (actionString != null && !actionString.isEmpty())
            deepLinkActionType = DeepLinkActionType.valueOf(Integer.valueOf(actionString));

        Log.d("DEEPLINK", "deep link action type: " + deepLinkActionType + "\tPNO: " + pno + "\tZIP: " + zip);

我的深层链接 URI 如下所示:

http://www.mywebsite.com/?action=1&amp;nmbr=09981122341088&amp;zip=11000

http://www.mywebsite.com/?action=1&amp;num=09981122341088

http://www.mywebsite.com/?action=3

欢迎关注this link

【讨论】:

  • 静止数据就像“www.myapp.com/...”未格式化。当您单击 Google 网络移动应用搜索结果中的链接时,您会收到格式化的链接吗?它与我的网页端代码有关吗?您是否只将链接 href 添加到您的网站代码?
  • @Turquoise 好的,我希望这对你有所帮助。使用pathPattern 将只返回/ 字符后面的查询,您需要解析它并删除您需要的参数(或可能在那里找到)。在我的示例中,我有多达 3-4 个副本,因为糟糕的后端会时不时地以不同的方式发送它们。
  • 当您在网页的 head 标签下键入
  • 是的,我得到了这样的完整链接。我得到了与我粘贴的相同的 3 个 URI。
  • 但是如果我们将链接 href 设置为 "android-app://com.example.android/mywebsite/action=3" 为什么我们会收到类似 "mywebsite.com/?action=3" 的 url?我的意思是我认为我可以收到像“android-app://com.example.android/mywebsite/action=3”这样的格式化网址。对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多