1

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
6
  • Are row and column integer values in your JavaScript code? Do you see any messages in LogCat? Returning a String is no problem -- I have sample code that does that. Commented Mar 17, 2012 at 12:19
  • If you have sample code for String please give in solution. Commented Mar 17, 2012 at 12:23
  • This directory contains a pair of projects that use addJavaScriptInterface(): github.com/commonsguy/cw-advandroid/tree/master/WebView Commented Mar 17, 2012 at 12:46
  • I made a mistake in javascript. the problem is solved. Working int data type. Commented Mar 17, 2012 at 12:48
  • @CommonsWare, I modified GeoWeb1 to print right after addJavascriptInterface, using browser.loadUrl("javascript:console.log(locater);");. That call prints out "Uncaught ReferenceError: locater is not defined". The same call later on, in the onResume() method, starts printing the object: com.commonsware.android.geoweb.GeoWebOne$Locater@416b0000 . (Gist of the file with the console output change: gist.github.com/3057175). Is there any way to tell how long it should take for the new object to become available? Commented Jul 5, 2012 at 23:49

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.