Set AP data

更新时间:
复制 MD 格式

The setAPDataStorage interface saves a string to unified client storage. The string length cannot exceed 200 × 1024.

Note
  • The underlying storage service is implemented differently on iOS and Android. The unified storage component for Android does not support the type=user property. For consistency with the frontend interface, if you set type=user, the Android underlying layer modifies the key to key=key + "_" +MD5(userId + userId + userId) before storing the data. The client must construct the key in the same way to retrieve the data.

  • In baselines 10.1.60 and earlier, configure the client to allow the interface to retrieve the userId. Otherwise, the storage interface cannot store data separately for each userId. For more information, see Implement the H5LoginProvider interface.

  • In baselines 10.1.68 and later, the userId defaults to the value set by MPLogger.setUserId. If H5LoginProvider is implemented, its value is used instead.

Implement the H5LoginProvider interface

Android

Implement the H5LoginProvider interface and set the instance class in H5ProviderManager.

Code example

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 {
    // Omit other code

    @Override
    public String getUserId() {
        // This method returns the userId
        return LoggerFactory.getLogContext().getUserId();
    }

    // Omit other code
}

Set H5LoginProvider

H5Utils.setProvider(H5LoginProvider.class.getName(), new H5LoginProviderImpl());

How to use the setAPDataStorage interface

AlipayJSBridge.call('setAPDataStorage', {
  type: "common",
  business: "customBusinessKey",
  key: "customKey",
  value: "customValue"
}, function(result) {
  alert(JSON.stringify(result));
});

Code example

<button id="J_saveDataBtn" class="btn">Save data</button>
<button id="J_getDataBtn" class="btn">View data</button>
<button id="J_removeDataBtn" class="btn">Delete data</button>

<script>
function ready(callback) {
  // If jsbridge is already injected, call it directly.
  if (window.AlipayJSBridge) {
    callback && callback();
  } else {
    // If not, listen for the injection event.
    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>

API reference

AlipayJSBridge.call('setAPDataStorage', {
  type, business, key, value
});

Request parameters

Property

Type

Description

Required

Default value

type

String

Whether to store data by user (user) or in common storage (common).

N

"common"

business

String

A custom business identifier that must match the one used in the corresponding client code.

In Android, this business identifier corresponds to the GROUP_ID passed when creating an APSharedPreferences instance.

No

"NebulaBiz"

key

String

The key for the custom data.

Yes

""

value

String

The value to store. Only strings are supported. JSON-formatted data must be converted to a string first.

Yes

""

Response parameters

The callback function receives a result: {success} parameter.

Property

Type

Description

success

bool

Indicates whether the data was saved successfully.

Error codes

Error code

Description

11

The string length exceeds the limit.