小编Sin*_*dhu的帖子

如何以类似方式从JavaScript调用Android和iOS中的函数

我正在尝试从androidiOS Swift(WKWebview)中调用函数javascript

对于Android:

public class WebAppInterface {

    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void postMessage(String message) {
        Log.v(TAG, "message ----"+message);
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();

    }
}

webView.addJavascriptInterface(new WebAppInterface(mContext), "appInterface");
Run Code Online (Sandbox Code Playgroud)

Android的JavaScript调用:

window.appInterface.postMessage("Hello);
Run Code Online (Sandbox Code Playgroud)

iOS的Java语言调用:

window.webkit.messageHandlers.appInterafce.postMeesage("Hello");
Run Code Online (Sandbox Code Playgroud)

因此,我们在这里从javascriptfor android和发出不同的调用iOS。是否有可能调用函数在这两个androidiOS一个单一的方式?

谢谢。

javascript android webview ios wkwebview

4
推荐指数
1
解决办法
2650
查看次数

如何使用 JSONObjects 使用 junit mockito 编写本地测试

我有一个使用 JSONObject 的函数,我需要测试它。这是我的代码:

这是我想测试的代码:

public String getJsonData() {
try {
    InputStream is = mContext.getAssets().open("chartInfo.json");
    int size = is.available();
    byte[] buffer = new byte[size];
    if (is.read(buffer) > 0)
        jsonString = new String(buffer, "UTF-8");
    is.close();

} catch (IOException ex) {
    ex.printStackTrace();
    return null;
}
return jsonString;
}

public String getChartTypeJS() {
jsonString = getJsonData();
try {
    JSONObject jsonObject = new JSONObject(jsonString);
    JSONObject javascriptEvent_JsonObject = jsonObject.getJSONObject("javascript_events");
    return javascriptEvent_JsonObject.getString("chartType");
} catch (JSONException e) {
    e.printStackTrace();
}
return "";
}
Run Code Online (Sandbox Code Playgroud)

我的测试代码:

@RunWith(MockitoJUnitRunner.class)
public class LoadJsonData_Test …
Run Code Online (Sandbox Code Playgroud)

java junit android unit-testing mockito

0
推荐指数
1
解决办法
4732
查看次数

标签 统计

android ×2

ios ×1

java ×1

javascript ×1

junit ×1

mockito ×1

unit-testing ×1

webview ×1

wkwebview ×1