setAPDataStorage
接口用于保存一个字符串到客户端统一存储,字符串长度不得超过 200×1024。
type=user
属性,为了与前端接口一致,当设置 type=user
时,Android 底层会设置为 key=key + “_” +MD5(userId + userId + userId)
并进行存储。业务用客户端取的时候也要对 key 做相应处理。实现 H5LoginProvider
接口,并将实例类设置到 H5ProviderManager
中。
package com.mpaas.nebula.provider;
import android.os.Bundle;
import com.alipay.mobile.common.logging.api.LoggerFactory;
import com.alipay.mobile.nebula.provider.H5LoginProvider;
public class H5LoginProviderImpl implements H5LoginProvider {
// 其他代码省略
@Override
public String getUserId() {
// 此方法返回 userId 即可
return LoggerFactory.getLogContext().getUserId();
}
// 其他代码省略
}
H5Utils.setProvider(H5LoginProvider.class.getName(), new H5LoginProviderImpl());
AlipayJSBridge.call('setAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey",
value: "customValue"
}, function(result) {
alert(JSON.stringify(result));
});
<button id="J_saveDataBtn" class="btn">保存数据</button>
<button id="J_getDataBtn" class="btn">查看数据</button>
<button id="J_removeDataBtn" class="btn">删除数据</button>
<script>
function ready(callback) {
// 如果 jsbridge 已经注入则直接调用
if (window.AlipayJSBridge) {
callback && callback();
} else {
// 如果没有注入则监听注入的事件
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
ready(function() {
document.querySelector('#J_saveDataBtn').addEventListener('click', function(e) {
AlipayJSBridge.call('setAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey",
value: "customValue"
}, function(result) {
alert(JSON.stringify(result));
});
}, false);
document.querySelector('#J_getDataBtn').addEventListener('click', function(e) {
AlipayJSBridge.call('getAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey"
}, function(result) {
alert(JSON.stringify(result));
});
}, false);
document.querySelector('#J_removeDataBtn').addEventListener('click', function(e) {
AlipayJSBridge.call('removeAPDataStorage', {
type: "common",
business: "customBusinessKey",
key: "customKey"
}, function(result) {
alert(JSON.stringify(result));
});
}, false);
}, false);
</script>
AlipayJSBridge.call('setAPDataStorage', {
type, business, key, value
});
名称 | 类型 | 描述 | 必选 | 默认值 |
---|---|---|---|---|
type | String | (user/common) 用户维度存储还是公共存储 | N | “common” |
business | String | 自定义的业务标识,可与相应的客户端存取代码约定,默认值为:NebulaBiz。 (在 Android 中,该业务标识与创建 APSharedPreferences 时所传入的 GROUP_ID 对应) |
N | “” |
key | String | 自定义数据的 key | Y | “” |
value | String | 需要存储的值,仅支持字符串类型。JSON 数据需要先字符串化 | Y | “” |
回调函数带入的参数 result: {success}
。
名称 | 类型 | 描述 |
---|---|---|
success | bool | 是否保存成功 |
错误码 | 描述 |
---|---|
11 | 字符串长度超限 |
在文档使用中是否遇到以下问题
更多建议
匿名提交