android - 无法查询存储在 Firebase 动态/深层链接中的参数

标签 android firebase uri deep-linking firebase-dynamic-links

我正在像这样手动创建一个 Firebase 动态/深层链接:

Uri BASE_URI = Uri.parse("http://example.com/");
String packageName = getBaseContext().getPackageName();
Uri APP_URI = BASE_URI.buildUpon().path(requestID.getText().toString().trim()+"%3DrequestID="+requestID.getText().toString().trim()+"%3Dextra1="+extra1.getText().toString().trim()+"%3Dextra2="+extra2.getText().toString().trim()).build();
String encodedUri = URLEncoder.encode(APP_URI.toString(), "UTF-8");
Uri deepLink = Uri.parse("https://appcode.app.goo.gl/?link="+encodedUri+"&apn="+packageName+"&amv="+16+"&ad="+0);

然后我与另一个用户共享它,他收到此链接:http://example.com/-KcP1YHgGsg3tVYybX8b%253DrequestID%3D-KcP1YHgGsg3tVYybX8b%253Dextra1%3Dtext1%253Dextra2%3D3%253Dtext2.

然后使用以下代码尝试从此处获取查询参数(链接中的额外参数):

    boolean autoLaunchDeepLink = false;
            AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
                    .setResultCallback(
                            new ResultCallback<AppInviteInvitationResult>() {
                                @Override
                                public void onResult(@NonNull AppInviteInvitationResult result) {
                                    if (result.getStatus().isSuccess()) {
                                        // Extract deep link from Intent
                                        Intent intent = result.getInvitationIntent();
                                        final String deepLink = AppInviteReferral.getDeepLink(intent);
                                        Log.d("deepLinkMainActivity", deepLink);

                                        Uri uri = Uri.parse(deepLink);
                                        String requestId = uri.getQueryParameter("requestID");
                                        Log.d("requestId123", requestId);
                                } else {
                                    Log.d("getInvitation", "getInvitation: no deep link found.");
                                }
                            }
                        });

但应用程序正在崩溃显示:java.lang.NullPointerException: println needs a message 因为 uri.getQueryParameter("requestID"); 正在返回

我哪里做错了,我如何深层链接获取查询参数?

请告诉我。

最佳答案


这是你的网址
Uri APP_URI = BASE_URI.buildUpon().path(requestID.getText().toString().trim()+"%3DrequestID="+requestID.getText().toString().trim()+"% 3Dextra1="+extra1.getText().toString().trim()+"%3Dextra2="+extra2.getText().toString().trim()).build();

如果你解码这个 Url 你会收到这样的东西

http://www.airbanq.com?name=45=extra1=45=extra2=12=extra3=455

此值 %3D 等于 =。要连接查询字符串中的值,您需要使用此 &,也许您可​​以尝试避免编码和解码,然后重试。我在另一个线程中建议了该选项,因为这对我来说适用于旧版本的 FIrebase。

这是你的空错误背后的主要原因,基本上你试图从中获取一个查询字符串 http://www.airbanq.com?name=45=extra1=45=extra2=12=extra3=455

创建这样的链接 http://www.airbanq.com?name=45&extra1=45&extra2=12&extra3=455 让我知道。在对您的代码进行此更改后,这将可以正常工作。

更新 1
我将与您分享一些代码,希望这可以解决您的问题。

这段代码是基于你现在拥有的
如您所见,第一部分是创建 URL 并添加一些参数

Uri BASE_URI = Uri.parse("http://example.com/");

Uri APP_URI = BASE_URI.buildUpon().appendQueryParameter("requestID", "200").
                    appendQueryParameter("extra1", "value").
                    appendQueryParameter("extra2", "value").build();


String encodedUri = null;
try {
   encodedUri = URLEncoder.encode(APP_URI.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
Uri deepLink = Uri.parse("https://app.app.goo.gl/?link="+encodedUri+"&apn=com.xx.xx.dev");

这最后一部分只是接收深层链接并读取值

AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
                .setResultCallback(
                        new ResultCallback<AppInviteInvitationResult>() {
                            @Override
                            public void onResult(AppInviteInvitationResult result) {
                                Log.d(TAG, "getInvitation:onResult:" + result.getStatus());
                                if (result.getStatus().isSuccess()) {
                                    // Extract information from the intent
                                    Intent intent = result.getInvitationIntent();
                                    String deepLink = AppInviteReferral.getDeepLink(intent);
                                    String invitationId = AppInviteReferral.getInvitationId(intent);

                                    try {
                                        deepLink = URLDecoder.decode(deepLink, "UTF-8");
                                    } catch (UnsupportedEncodingException e) {
                                        e.printStackTrace();
                                    }
                                    Uri uri = Uri.parse(deepLink);
                                    String requestId = uri.getQueryParameter("requestID");
                                    String requestId2 = uri.getQueryParameter("extra2");

                                    // Because autoLaunchDeepLink = true we don't have to do anything
                                    // here, but we could set that to false and manually choose
                                    // an Activity to launch to handle the deep link here.
                                    // ...
                                }
                            }
                        });

最后这是 list

<activity
            android:name="com.google.firebase.quickstart.invites.MainActivity"
            android:label="@string/app_name">
            <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:host="example.com" android:scheme="http"/>
                <data android:host="example.com" android:scheme="https"/>

            </intent-filter>
        </activity>

希望能解决你的问题

关于android - 无法查询存储在 Firebase 动态/深层链接中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42119692/

相关文章:

android - ListView 中的垂直和水平滚动

android - 从 Firebase 获取项目的位置

Java URI 类 : constructor determines whether or not query is encoded?

c# - 具有哈希和 Uri LocalPath 处理的文件名

android - 如何在monkeyrunner测试文件中导入一个py文件函数

android - 如何从 Facebook SDK 4.5 中的 Android 应用程序获取用户的个人资料图片、主要电子邮件、Facebook 链接?

ios - Firebase 是否仍支持 Mac OS X - 2016 年 7 月

android - 如何监听自定义 URI

android - 定位在框架布局内

java - 从 firestore 中检索所有文档作为自定义对象