我正在使用优步API,该API使用uber://?...
网址深入链接到Uber原生应用。我正在建立一个移动网站,我正在构建其中一个网址as described here;
在iOS上一切正常但在Android上Uber应用程序打开并且只获得其中一个参数product_id
。所以我认为我对如何编码URL以及Android系统如何打开它存在一些问题。这是我建立URL的JS:
uber.createURL = function() {
var params = {
"client_id": uber.CLIENT_ID,
"product_id": maps.product_id,
"pickup[latitude]": maps.noSurgeMarker.getPosition().lat(),
"pickup[longitude]": maps.noSurgeMarker.getPosition().lng(),
"dropoff[latitude]": maps.destMarker.getPosition().lat(),
"dropoff[longitude]": maps.destMarker.getPosition().lng(),
"pickup[formatted_address]": $('#pickup').val(),
"dropoff[formatted_address]": $('#destination').val()
};
var url = 'uber://?action=setPickup';
for (var key in params) {
if (params.hasOwnProperty(key)) {
url += ('&' + key + '=' + encodeURIComponent(params[key]));
}
}
return url;
};
然后我调用以下代码打开链接:
var url = uber.createURL();
window.location.href = url;
我错过了一些明显的东西吗?同样,这适用于iOS,但不适用于Android。另外奇怪的是,如果我在计算机上使用createURL
生成URL,使用PushBullet将其发送到我的Android设备,打开它可以正常工作。但是,如果我通过Chrome for Android获取网址,Uber应用程序就会打开并且只有product_id
正确,而不是任何提取或下降部分。
注意:我已经使用jQuery的params
函数进行了修改,但是效果不佳,这就是我进入手动for (var key in params) {...}
循环的原因。
答案 0 :(得分:3)
好吧我发现这不是编码问题,而是Chrome如何启动应用程序的意图。这是Pushbullet的一个意图,这个工作正常:
I/ActivityManager( 942): START u0
{act=android.intent.action.VIEW
dat=uber://?
client_id=REDACTED&
action=setPickup&
product_id=a1111c8c-c720-46c3-8534-2fcdd730040d&
pickup[latitude]=37.780654&
pickup[longitude]=-122.405599&
dropoff[latitude]=37.7970558&
dropoff[longitude]=-122.43199099999998
flg=0x10000000 cmp=com.ubercab/.client.feature.launch.LauncherActivity} from pid 19023
这是Chrome的一个意图,这个推出优步应用程序,但没有任何数据被应用程序解析(它只是打开,好像我点击了启动器图标):
I/ActivityManager( 942): START u0
{act=android.intent.action.VIEW
cat=[android.intent.category.BROWSABLE]
dat=uber://?
client_id=REDACTED&
action=setPickup&
product_id=a1111c8c-c720-46c3-8534-2fcdd730040d&
pickup[latitude]=37.780654&
pickup[longitude]=-122.405599&
dropoff[latitude]=37.7970558&
dropoff[longitude]=-122.43199099999998
flg=0x10000000 cmp=com.ubercab/.client.feature.launch.LauncherActivity (has extras)} from pid 26446
您可以看到唯一的区别是添加的cat=[android.intent.category.BROWSABLE]
,这是从Chrome for Android(reference)启动的意图所需的内容。
总的来说,我认为这是优步必须要解决的问题。现在,如果我希望它可以运行,我似乎必须将我的网站打包到Android应用中的WebView
。
答案 1 :(得分:2)
你需要在key和params [key]上调用encodeUriComponent
。