带有Parameters的WebView addJavascriptInterface函数

时间:2012-03-17 12:14:16

标签: javascript android android-webview

我已经为WebView添加了一个JavaScript接口。

我可以使用所有没有参数的函数。

但是当我从JavaScript中提供参数时。 WebView不会调用该函数。

参见代码

的Javascript

        function getCellString(row, column) {
            return Report.getCellString(row,column);
        }

web视图

webView.addJavascriptInterface(new JavaScriptInterface(), "Report");

Javascript界面​​

public class JavaScriptInterface
{
public String getCellString(int row, int column)
{
    return row + "," + column;
}
}

我不确定是因为String返回类型还是函数的参数。

EDITED

我尝试不给getCellString()提供任何参数仍然没有被调用。

这意味着问题在于返回类型。我给出了String返回类型,它不是原始数据类型。

任何人都可以告诉我应该用什么数据类型代替String,以便它接受。

addJavaScriptInterface后的LogCat

   03-17 17:52:26.748: V/webcore(19908): ADD_JS_INTERFACE arg1=0 arg2=0 obj=android.webkit.WebViewCore$JSInterfaceData@44dfe378
03-17 17:52:26.858: V/webcore(19908): LOAD_URL arg1=0 arg2=0 obj=android.webkit.WebViewCore$GetUrlData@44dff760
03-17 17:52:26.858: V/webcore(19908):  CORE loadUrl file:///data/data/mypackage/cache/html_report/ReportHTML.html
03-17 17:52:26.858: V/webkit(19908): startLoadingResource: url=file:///data/data/mypackage/cache/html_report/ReportHTML.html, method=GET, postData=null, isMainFramePage=true, mainResource=true, userGesture=true
03-17 17:52:26.858: V/webkit(19908): LoadListener constructor url=file:///data/data/mypackage/cache/html_report/ReportHTML.html
03-17 17:52:26.868: V/webview(19908): WEBCORE_INITIALIZED_MSG_ID
03-17 17:52:26.868: V/webkit(19908): LoadListener: from: file:///data/data/mypackage/cache/html_report/ReportHTML.html major: 1 minor: 1 code: 200 reason: OK
03-17 17:52:26.878: V/webkit(19908): LoadListener.headers
03-17 17:52:26.878: V/webcore(19908): 200 arg1=0 arg2=0 obj=null
03-17 17:52:26.878: V/webkit(19908): LoadListener.data(): url: file:///data/data/mypackage/cache/html_report/ReportHTML.html
03-17 17:52:26.878: V/webkit(19908): LoadListener.data(): url: file:///data/data/mypackage/cache/html_report/ReportHTML.html
03-17 17:52:26.888: V/webkit(19908): LoadListener.endData(): url: file:///data/data/mypackage/cache/html_report/ReportHTML.html
03-17 17:52:26.898: V/webkit(19908): guessMimeTypeFromExtension: url = file:///data/data/mypackage/cache/html_report/ReportHTML.html
03-17 17:52:26.908: V/webview(19908): sendOurVisibleRect=(0,55,r=240,b=320
03-17 17:52:26.908: V/webview(19908): setCertificate=null
03-17 17:52:26.988: V/webkit(19908): LoadListener.detachRequestHandle(): requestHandle: null
03-17 17:52:27.098: V/webview(19908): SET_SCROLLBAR_MODES
03-17 17:52:27.098: V/webview(19908): SET_SCROLLBAR_MODES
03-17 17:52:27.098: V/webview(19908): SET_SCROLLBAR_MODES
03-17 17:52:27.098: V/webcore(19908): didFirstLayout standardLoad =true
03-17 17:52:27.108: V/webcore(19908): SET_SCROLL_OFFSET arg1=0 arg2=0 obj=Point(0, 0)
03-17 17:52:27.108: V/webview(19908): UPDATE_TEXT_ENTRY_MSG_ID
03-17 17:52:27.108: V/webcore(19908): SET_GLOBAL_BOUNDS arg1=0 arg2=0 obj=Rect(0, 55 - 240, 320)
03-17 17:52:27.118: V/webcore(19908): VIEW_SIZE_CHANGED arg1=0 arg2=0 obj=android.webkit.WebView$ViewSizeData@44d1d2c8
03-17 17:52:27.118: V/webcore(19908): viewSizeChanged w=313; h=353; textwrapWidth=313; scale=0.75
03-17 17:52:27.118: V/webcore(19908): viewSizeChanged
03-17 17:52:27.118: V/webcore(19908): SET_ACTIVE arg1=0 arg2=0 obj=null
03-17 17:52:27.128: V/webcore(19908): WEBKIT_DRAW arg1=0 arg2=0 obj=null
03-17 17:52:27.128: V/webcore(19908): webkitDraw start
03-17 17:52:27.128: V/webcore(19908): webkitDraw NEW_PICTURE_MSG_ID
03-17 17:52:27.128: V/webcore(19908): UPDATE_CACHE_AND_TEXT_ENTRY arg1=0 arg2=0 obj=null
03-17 17:52:27.128: V/webview(19908): NEW_PICTURE_MSG_ID
03-17 17:52:27.128: V/webview(19908): NEW_PICTURE_MSG_ID {0,0,313,353}
03-17 17:52:27.128: V/webview(19908): UPDATE_TEXT_ENTRY_MSG_ID
03-17 17:53:28.266: V/websync(19908): *** WebSyncManager sync ***
03-17 17:53:28.266: V/websync(19908): CookieSyncManager::syncFromRamToFlash STARTS
03-17 17:53:28.268: V/websync(19908): CookieSyncManager::syncFromRamToFlash DONE

1 个答案:

答案 0 :(得分:2)

如果您将方法参数从int更改为String,那么该函数应该可以在WebView中调用:

public String getCellString(String row, String column)
{
    return row + "," + column;
}