use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.
the class WXModuleManagerTest method testCallModuleMethod2.
@Test
public void testCallModuleMethod2() throws Exception {
JSONArray args = new JSONArray();
args.add("testarg");
WXModuleManager.callModuleMethod(instance.getInstanceId(), "test1", "testMethod", args);
}
use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.
the class WXWebsocketBridge method onMessage.
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) {
if (type != WebSocket.PayloadType.TEXT) {
WXLogUtils.w("Websocket received unexpected message with payload of type " + type);
return;
}
String message = null;
try {
message = payload.readUtf8();
JSONObject jsonObject = JSONObject.parseObject(message);
Object name = jsonObject.get("name");
Object value = jsonObject.get("value");
if (name == null || value == null) {
return;
}
if (name.toString().equals("callNative")) {
JSONArray jsonArray = JSONObject.parseArray(value.toString());
callNative(jsonArray.getString(0), jsonArray.getString(1), jsonArray.getString(2));
}
} catch (Exception e) {
} finally {
try {
payload.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.
the class WXWebSocketManager method onMessage.
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
if (type != WebSocket.PayloadType.TEXT) {
WXLogUtils.w("Websocket received unexpected message with payload of type " + type);
return;
}
for (JSDebuggerCallback callback : mCallbacks.values()) {
callback.onMessage(payload, type);
}
String message = null;
try {
message = payload.readUtf8();
JSONObject jsonObject = JSONObject.parseObject(message);
Object name = jsonObject.get("method");
Object value = jsonObject.get("arguments");
if (name == null || value == null) {
return;
}
if (TextUtils.equals(name.toString(), "setLogLevel")) {
JSONArray jsonArray = JSONObject.parseArray(value.toString());
String level = jsonArray.get(0).toString();
WXEnvironment.sLogLevel = LogLevel.valueOf(level.toUpperCase());
WXLogUtils.v("into--[onMessage]setLogLevel");
}
} catch (Exception e) {
} finally {
payload.close();
}
}
use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.
the class WXReflectionUtilsTest method testParseArgument.
@Test
public void testParseArgument() throws Exception {
Object value = WXReflectionUtils.parseArgument(String.class, "dkdkdkdk");
assertTrue(value instanceof String);
value = WXReflectionUtils.parseArgument(long.class, "123444");
assertTrue(value instanceof Long);
value = WXReflectionUtils.parseArgument(int.class, "123444");
assertTrue(value instanceof Integer);
value = WXReflectionUtils.parseArgument(Integer.class, "123444");
assertTrue(value instanceof Integer);
value = WXReflectionUtils.parseArgument(int.class, 123444);
assertTrue(value instanceof Integer);
value = WXReflectionUtils.parseArgument(double.class, Double.toString(123.444d));
assertTrue(value instanceof Double);
JSONObject j = new JSONObject();
j.put("a", "b");
j.put("c", 23);
value = WXReflectionUtils.parseArgument(String.class, j);
assertTrue(value instanceof String);
value = WXReflectionUtils.parseArgument(Map.class, j);
assertTrue(value instanceof Map);
assertEquals(((Map) value).get("a"), "b");
value = WXReflectionUtils.parseArgument(JSONObject.class, j);
assertTrue(value instanceof JSONObject);
assertEquals(((JSONObject) value).get("a"), "b");
assertEquals(((JSONObject) value).get("c"), 23);
value = WXReflectionUtils.parseArgument(JSONObject.class, JSON.toJSONString(j));
assertTrue(value instanceof JSONObject);
assertEquals(((JSONObject) value).get("a"), "b");
assertEquals(((JSONObject) value).get("c"), 23);
JSONArray k = new JSONArray();
k.add("b");
k.add(23);
value = WXReflectionUtils.parseArgument(String[].class, k);
assertTrue(value instanceof String[]);
assertEquals(((String[]) value)[0], "b");
assertEquals(((String[]) value)[1], "23");
value = WXReflectionUtils.parseArgument(String[].class, JSON.toJSONString(k));
assertTrue(value instanceof String[]);
assertEquals(((String[]) value)[0], "b");
assertEquals(((String[]) value)[1], "23");
value = WXReflectionUtils.parseArgument(List.class, JSON.toJSONString(k));
assertTrue(value instanceof List);
assertEquals(((List) value).get(0), "b");
assertEquals(((List) value).get(1), 23);
k = new JSONArray();
k.add(1);
k.add(23);
value = WXReflectionUtils.parseArgument(int[].class, JSON.toJSONString(k));
assertTrue(value instanceof int[]);
assertEquals(((int[]) value)[0], 1);
assertEquals(((int[]) value)[1], 23);
}
use of com.alibaba.fastjson.JSONArray in project weex-example by KalicyZhou.
the class WXBridgeManager method callNative.
/**
* Dispatch the native task to be executed.
*
* @param instanceId {@link WXSDKInstance#mInstanceId}
* @param tasks tasks to be executed
* @param callback next tick id
*/
public int callNative(String instanceId, String tasks, String callback) {
if (TextUtils.isEmpty(tasks)) {
if (WXEnvironment.isApkDebugable()) {
WXLogUtils.e("[WXBridgeManager] callNative: call Native tasks is null");
}
commitJSBridgeAlarmMonitor(instanceId, WXErrorCode.WX_ERR_INVOKE_NATIVE, "[WXBridgeManager] callNative: call Native tasks is null");
return IWXBridge.INSTANCE_RENDERING_ERROR;
}
if (WXEnvironment.isApkDebugable()) {
mLodBuilder.append("[WXBridgeManager] callNative >>>> instanceId:").append(instanceId).append(", tasks:").append(tasks).append(", callback:").append(callback);
WXLogUtils.d(mLodBuilder.substring(0));
mLodBuilder.setLength(0);
}
if (mDestroyedInstanceId != null && mDestroyedInstanceId.contains(instanceId)) {
return IWXBridge.DESTROY_INSTANCE;
}
long start = System.currentTimeMillis();
JSONArray array = JSON.parseArray(tasks);
if (WXSDKManager.getInstance().getSDKInstance(instanceId) != null) {
WXSDKManager.getInstance().getSDKInstance(instanceId).jsonParseTime(System.currentTimeMillis() - start);
}
int size = array.size();
if (size > 0) {
try {
JSONObject task;
for (int i = 0; i < size; ++i) {
task = (JSONObject) array.get(i);
if (task != null && WXSDKManager.getInstance().getSDKInstance(instanceId) != null) {
Object target = task.get(MODULE);
if (target != null) {
if (WXDomModule.WXDOM.equals(target)) {
WXDomModule dom = getDomModule(instanceId);
dom.callDomMethod(task);
} else {
WXModuleManager.callModuleMethod(instanceId, (String) target, (String) task.get(METHOD), (JSONArray) task.get(ARGS));
}
} else if (task.get(COMPONENT) != null) {
//call component
WXDomModule dom = getDomModule(instanceId);
dom.invokeMethod((String) task.get(REF), (String) task.get(METHOD), (JSONArray) task.get(ARGS));
} else {
throw new IllegalArgumentException("unknown callNative");
}
}
}
} catch (Exception e) {
WXLogUtils.e("[WXBridgeManager] callNative exception: ", e);
commitJSBridgeAlarmMonitor(instanceId, WXErrorCode.WX_ERR_INVOKE_NATIVE, "[WXBridgeManager] callNative exception " + e.getCause());
}
}
if (UNDEFINED.equals(callback) || NON_CALLBACK.equals(callback)) {
return IWXBridge.INSTANCE_RENDERING_ERROR;
}
// get next tick
getNextTick(instanceId, callback);
return IWXBridge.INSTANCE_RENDERING;
}
Aggregations