uniapp与android的数据交互
·
标题
测试uniapp和安卓原生数据的交互,看看weex会怎么转化这些数据
也可以直接传数据过来,**的uniapp官网什么都不说。
直接注解打上,传String过来也是可以的。
@UniJSMethod
fun test(params: JSONObject, cbk: UniJSCallback) {
Log.d(TAG, "test: ")
if (Build.VERSION.SDK_INT >= 24) {
params.forEach { key, value ->
Log.d(TAG, "test: $key, $value")
when (value) {
is String -> Log.d(TAG, "test: $key, (String)$value")
is Int -> Log.d(TAG, "test: $key, (Int)$value")
is Boolean -> Log.d(TAG, "test: $key, (Boolean)$value")
is Float -> Log.d(TAG, "test: $key, (Float)$value")
is Double -> Log.d(TAG, "test: $key, (Double)$value")
is Long -> Log.d(TAG, "test: $key, (Long)$value")
is Short -> Log.d(TAG, "test: $key, (Short)$value")
is Byte -> Log.d(TAG, "test: $key, (Byte)$value")
is Char -> Log.d(TAG, "test: $key, (Char)$value")
is JSONArray -> {
Log.d(TAG, "test: $key, (JSONArray)$value")
value.forEach { v ->
Log.d(TAG, "test: (JSONArray internal)$v")
}
}
is ByteArray -> {
Log.d(TAG, "test: $key, (ByteArray)${value.toHexString()}")
}
is JSONObject -> {
Log.d(TAG, "test: $key, (JSONObject)$value")
value.forEach { k, v ->
Log.d(TAG, "test: (JSONObject internal)$k, $v")
}
}
}
}
}
cbk.invoke(JSONObject().apply {
put("int1", 1)
put("byteArray", byteArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
put("lists", listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
put("sets", setOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
})
}
const meshp = uni.requireNativePlugin('mesh');
meshp.test({
byteArray: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
jsonArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
string: 'Hello World',
int: 1234567890,
double: 3.141592653589793,
boolean: true,
object: {
name: 'John',
age: 30,
city: 'New York'
},
},res=>{
console.log("meshConnecter", res)
console.log("meshConnecter", res.byteArray)
})
交互打印
uniapp层 省略,其中 byteArray,lists,sets返回都是jsonArray.
android层,
foreach会打印一遍
test: boolean, true
test: boolean, (Boolean)true
test: string, Hello World
test: string, (String)Hello World
test: double, 3.141592653589793
test: double, (Double)3.141592653589793
test: byteArray, {"11":12,"12":13,"13":14,"14":15,"15":16,"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7,"7":8,"8":9,"9":10,"10":11}
test: byteArray, (JSONObject){"11":12,"12":13,"13":14,"14":15,"15":16,"0":1,"1":2,"2":3,"3":4,"4":5,"5":6,"6":7,"7":8,"8":9,"9":10,"10":11}
test: object, (JSONObject){"city":"New York","name":"John","age":30}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)