How to deep link json data from server on Android

Answered Asked on May 29, 2016 in Technical.

I have an app that gets all of its products from a remote database and api. I want the user to be able to click the deep link and open the app to show that products details. How would I go about doing this.

The way the app works is that it has a listview in the main activity that receives its list of data from a remote server. When the user clicks on a product, the parameters are passed to the detail class for viewing. The detail class doesn’t access the remote api.

CurTro Studios — Developer
1 Answer(s)
Best answer Answered on May 30, 2016.

On an abstract level, all you need to do is somehow know which product to ‘click’ when the user comes in via a deep link.  You probably can’t take advantage of the automatic deep link routing approach, but it should be fairly easy to encode an identifying key/value pair into the link and then use that to tell the main activity which parameters to retrieve and pass into the detail class.

Branch — Developer
Commented on May 30, 2016.

Is it possible to initiate a “virtual” click on a list item in the main activity using a deep link?

CurTro Studios — Developer
Commented on May 30, 2016.

That would be up to your specific implementation, but I it could work that way if you wanted.  Deep links just pass data into the app, so the actual handling of what happens next is your choice.

To answer a different way, deep links are just a delivery method. UPS doesn’t decide what to do with your package after you get it, but they’ll make sure it always arrives.

Branch — Developer
Commented on May 31, 2016.

Point taken. 😀

Ok. I think I figured out how I can display the detail class based on the incoming branch link. I noticed you have examples for “sharing” a link but do you have any examples for “receiving” the link?

Also, I need a better straight forward example to sharing a link that includes the product name and id. I’m extremely new to this whole deep linking concept but see the potential and would really like to implement it. Thank you.

CurTro Studios — Developer
Commented on May 31, 2016.

Great question! The literal ‘receiving’ part actually happens as soon as you initialize a Branch session. All the parameters you get in that callback are the raw data from whatever link was opened and you can do whatever you want with them after that — sky’s the limit :).

For a basic example of how many of our partners implement this, you can check out the automatic deep link routing guide. An example of more customized routing logic is also available here. These may not directly carry across since you’ve got a somewhat unusual configuration with your parent and detail classes, but the general idea should be close.

For an example of creating a link, you can check the creating links guide. You would probably want to set two metadata parameters, one each for product name and product ID.

Branch — Developer
Commented on June 1, 2016.

@Alex Bauer Ok.  I figured out how to get deep link working but I’m running into some issues I hope you can help me with.

In my MainActivity’s onStart method, the Branch instance show the received params in the logcat:

BranchConfigTest: deep link data: {“sid”:”1″,…

In the onResume  method I’m trying to get the specific string “sid” value. Right now just with a Toast. I’ll figure out how to use the value later. Right now I’m just trying to make sure I can capture that specific value. Here’s what I have in my onResume.

if (Branch.isAutoDeepLinkLaunch(this)) {

try {

String autoDeeplinkedValue = Branch.getInstance().getLatestReferringParams().getString("sid");

Toast.makeText(this, "Opened with deep link" + "sid=" + autoDeeplinkedValue, Toast.LENGTH_LONG).show();

Log.i(AppConfig.TAG_DEF, "SID= " + autoDeeplinkedValue);

} catch (JSONException e) {

e.printStackTrace();

}

} else {

Toast.makeText(this, "normal launch", Toast.LENGTH_LONG).show();

}

Unfortunately, when I click on the link shared from within the app in my email, the app launches showing the toast after “else”. “normal launch”. I’m trying to figure out why it won’t show the deep link value toast. I have everything setup exactly as it states in the guide.

On a side note, when I set the share channel to Facebook, the post “window” doesn’t show any of the information shared from within the app. It’s blank. Yet if I choose email or text, it shows the link. Any help you can give on that as well would be much appreciated.

CurTro Studios — Developer
Commented on June 2, 2016.

Could you also share the section from your AndroidManifest.xml file where you define the activity where that onResume() is implemented? We’re looking for a line like this:

 <meta-data android:name="io.branch.sdk.auto_link_keys" android:value="product_picture" />
 

See here for more about why this matters: https://dev.branch.io/getting-started/deep-link-routing/guide/android/#register-activity-for-deep-link-routing

Branch — Developer
Commented on June 2, 2016.

Regarding the Facebook content prefilling, this is actually against Facebook’s new policy. You can read more about that here and see the official policy doc here. Annoying, I know…

 

Branch — Developer
Commented on June 2, 2016.

I added the meta-data you posted. I must’ve missed that point in the guide. Still not getting the key though. When clicking on the created/shared link, the app opens and shows the toast “normal launch”. Logcat still shows the received data and I can see the string I’m trying to retrieve. I have my MainActivity receiving the deep link. It has the onResume and onStart declarations. Here’s how it looks in my Manifest. Please reply as soon as you can. I have everything set up to launch my app. I just need to get that received string. Then I’m good to go. Thank you for your help!!

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:launchMode="singleTask"
    android:theme="@style/AppTheme.NoActionBar">
    
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" android:host="z9bs.app.link" />
    </intent-filter>
    <meta-data android:name="io.branch.sdk.auto_link_keys" android:value="sid" />
</activity>
CurTro Studios — Developer
Commented on June 2, 2016.

I figured it out. I just created a separate detail class that would only show the details for the product id that is being received from the deep link. Works perfectly now. Thank you for all your help! I greatly appreciate it.

CurTro Studios — Developer
Commented on June 2, 2016.

One last question, if I use .setChannel(“twitter”) do I have to have a Twitter key for it to work?

CurTro Studios — Developer
Commented on June 2, 2016.

Fantastic! I’m so glad to hear it’s working well now 🙂

You won’t need a Twitter key to use .setChannel(“twitter”) because you’re not actually doing any posting yourself — you’re just launching the user’s copy of the Twitter app (if it is installed) with some content pre-filled.

Branch — Developer
Commented on June 5, 2016.

If anyone reads this and would like to know how to share a deep link for data coming from a remote database, I would be more than happy to share how I did it.

CurTro Studios — Developer