Unable to query parameters stored in a Firebase dynamic/deep-link

五迷三道 提交于 2019-12-01 09:19:57

问题


I'm creating a Firebase dynamic/deep link manually like this:

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);

and then I'm sharing it with another user and he is receiving this link: http://example.com/-KcP1YHgGsg3tVYybX8b%253DrequestID%3D-KcP1YHgGsg3tVYybX8b%253Dextra1%3Dtext1%253Dextra2%3D3%253Dtext2.

Then using following code I'm trying to get the query parameters (extra parameters in the link) from here:

    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.");
                                }
                            }
                        });

but the app is getting crashed showing: java.lang.NullPointerException: println needs a message because uri.getQueryParameter("requestID"); is returning null.

What am I doing wrong and How can I get the query parameters from the deep-link?

Please let me know.


回答1:



This i your url
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();

If you decode this Url you are going to receive something like this

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

This value %3D is equal to a =. To concatenate values in a query string you need to use this &, maybe you can try to avoid the encoding and decoding and try again. I suggested that option, in the other thread, because that was working fine to me with an older version of FIrebase.

That's the main reason behind of your null error, basically you are trying to get a query string from this http://www.airbanq.com?name=45=extra1=45=extra2=12=extra3=455

Create a link like this http://www.airbanq.com?name=45&extra1=45&extra2=12&extra3=455 and let me know. After this change with your code this will be works fine.

Update 1
I will share with you some code, hope this can solve your problem.

This code is based on what you have right now
This first part is to create the URL and add some parameters as you can see

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");

This last part is just to receive the deeplink and read the values

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.
                                    // ...
                                }
                            }
                        });

And finally this is the manifest

<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>

Hope this can solve your problems




回答2:


If you want to extract a text from the link text, use regex or get at specific indexof text. This is how you can get the data from invitation: Reference

Intent intent = result.getInvitationIntent();
String deepLink = AppInviteReferral.getDeepLink(intent);
String invitationId = AppInviteReferral.getInvitationId(intent);

and it s better to paste whole error message.



来源:https://stackoverflow.com/questions/42119692/unable-to-query-parameters-stored-in-a-firebase-dynamic-deep-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!