Search in sources :

Example 86 with JSONArray

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);
}
Also used : JSONArray(com.alibaba.fastjson.JSONArray) Test(org.junit.Test) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest)

Example 87 with JSONArray

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();
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) IOException(java.io.IOException) IOException(java.io.IOException)

Example 88 with JSONArray

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();
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) IOException(java.io.IOException)

Example 89 with JSONArray

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);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 90 with JSONArray

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;
}
Also used : WXDomModule(com.taobao.weex.dom.WXDomModule) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) WXException(com.taobao.weex.common.WXException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WXRuntimeException(com.taobao.weex.common.WXRuntimeException)

Aggregations

JSONArray (com.alibaba.fastjson.JSONArray)95 JSONObject (com.alibaba.fastjson.JSONObject)39 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 StringReader (java.io.StringReader)6 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)5 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)5 Reader (java.io.Reader)5 WXSDKInstanceTest (com.taobao.weex.WXSDKInstanceTest)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 BigDecimal (java.math.BigDecimal)3 ParseException (java.text.ParseException)3 Map (java.util.Map)3 Setting (org.aisen.android.common.setting.Setting)3 JSONReader (com.alibaba.fastjson.JSONReader)2 InputStream (java.io.InputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2