Integrate the operation module
The mini program operation module mainly includes data monitoring and release management, user interaction (such as message pushing, content delivery), and handling feedback and comments. The operation platform also supports permission management and multi-environment operations, ensuring the smooth flow of development, testing, and launch processes.
To realize the functions that are supported in the operation platform, integrate the operation module by the following steps.
Step 1. Add operation-releated dependencies
Add the operation-releated dependencies in the app-level build.gradle file with the following code.
Check the latest iapminiprogram_version
version in the Android Release Notes.
dependencies {
implementation "com.alipay.plus.android:iapminiprogram-operation:${iapminiprogram_version}"
}
Note: Initialization is not required, because the operation-related dependencies are automatically initialized during the initialization of the mini program SDK. You can directly call the corresponding methods within the mini program.
Step 2. Register the network proxy
Take the following code sample as references to register the network proxy class (DemoCommonOperationNetworkProxy
).
MiniProgramInitConfig initConfig = new MiniProgramInitConfig();
//ohter config
...
//register proxy
initConfig.registerNetworkProxy(new DemoCommonOperationNetworkProxy());
MiniProgram.init(MyApplication.get(), initConfig,new MiniProgramInitCallback() {
....
})
For the DemoCommonOperationNetworkProxy
class, see the following code sample.
public class DemoCommonOperationNetworkProxy implements OperationNetworkProxy {
private static final String TAG = "DemoNetworkProxy";
private static final String DEFAULT_PROD_REGION_SERVER = "https://yyy.xxx";
private static final int TIMEOUT = 5000;
@Override
public HttpProxyResponseInfo sendHttpRequest(HttpProxyRequestInfo proxyRequestInfo) {
JSONObject requestJson = new JSONObject();
requestJson.put("customerId", ""); // generate customerId
requestJson.put("proxyRequestHeader", proxyRequestInfo.getRequestHeader());
requestJson.put("proxyRequestData", proxyRequestInfo.getProxyRequestData());
String requestBody = new JSONArray(Collections.singletonList(requestJson)).toJSONString();
try {
HttpURLConnection connection = (HttpURLConnection) new URL(DEFAULT_PROD_REGION_SERVER).openConnection();
connection.setConnectTimeout(TIMEOUT);
connection.setReadTimeout(TIMEOUT);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setInstanceFollowRedirects(true);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("workspaceId", "default");
headers.put("appId", "xxxx");
headers.put("walletTenantId", ""); // get your wallet tenant id
headers.put("operation-type", "sdk.proxy.demo.test");
addHeaders(connection, headers);
try (DataOutputStream dos = new DataOutputStream(connection.getOutputStream())) {
dos.write(requestBody.getBytes(StandardCharsets.UTF_8));
dos.flush();
}
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
String result = streamToString(connection.getInputStream());
return new Gson().fromJson(result, new TypeToken<HttpProxyResponseInfo>() {}.getType());
} else {
Log.e(TAG, "Request failed with response code: " + connection.getResponseCode());
}
} catch (IOException e) {
Log.e(TAG, "sendHttpRequest error: ", e);
}
return null;
}
private static String streamToString(InputStream is) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return baos.toString();
} catch (IOException e) {
Log.e(TAG, "streamToString error: ", e);
return null;
}
}
private static void addHeaders(HttpURLConnection connection, Map<String, ?> headers) {
if (headers == null) return;
for (Map.Entry<String, ?> entry : headers.entrySet()) {
if (TextUtils.isEmpty(connection.getRequestProperty(entry.getKey()))) {
connection.setRequestProperty(entry.getKey(), String.valueOf(entry.getValue()));
}
}
}
}
Step 3. Call operation-specific APIs per your business requirements.
To implement specific operation-related services/capabilities, call APIs per your business requirements.