Vivek Khandelwal
Vivek Khandelwal

Reputation: 7849

WebView addJavascriptInterface function with Parameters

I have added a JavaScript Interface to WebView.

I am able to use all the Functions which has no Parameters.

But When i gave the Parameter from JavaScript. The function are not called by WebView.

See Code

Javascript

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

WebView

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

Javascript Interface

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

I am not sure whether it because String return type or the parameter for the function.

EDITED

I tried giving no Parameters to the getCellString() Still it is not being called.

That means problem is with return type. I gave the String return type which is not primitive data type.

Can any one tell me what Data Type should i give in place of String so that it accepts.

LogCat after addJavaScriptInterface

   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

Upvotes: 1

Views: 7429

Answers (1)

Boris Strandjev
Boris Strandjev

Reputation: 46943

If you change the method parameters from int to String then the function should be callable from within the WebView:

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

Upvotes: 3

Related Questions