anismiles / websocket-android-phonegap

This is a Java library that implements Websockt API (Draft-75/76) for Android platform. Library uses java.nio.* packages for efficient non-blocking evented behavior. It easily gets integrated with Phonegap framework.
http://wp.me/pVyFz-qO
MIT License
322 stars 137 forks source link

addJavascriptInterface not working #15

Open xli2012 opened 11 years ago

xli2012 commented 11 years ago

Hi,

I followed your instruction and built the sample phonegap app, but it doesn't work plus the log shows: E/Web Console( 1285): Uncaught TypeError: Object [object Object] has no method 'getInstance' at file:///android_asset/www/js/websocket.js:35

The main class looks like this: public class Grunt extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); final WebSocketFactory factory = new WebSocketFactory(appView); appView.addJavascriptInterface(factory, "WebSocketFactory"); loadUrl("file:///android_asset/www/websocket.html"); } } I have tried to swap the lines, calling init(), etc., none works. Do you have a thought? I have googled the whole night and couldn't find a solution, please give me some light!

Thanks!

anismiles commented 11 years ago

Current project was designed to work with Phonegap-0.9 version. Probably you are using some other version, and that's why you are seeing this problem. I didn't get a chance to update this project for newer Phonegap versions. Will do it whenever I have some time.

naoufalmed commented 11 years ago

Hi,

Maybe you have already figured it out how to solve (or work around) this issue, but this comment can help others who might get into the same issue.

This issue happens if your target API is Jelly Beans MR1 or higher. If this is the case, the WebSocketFactory class should be updated to follow the new guidelines of the Android API about the usage of the addJavascriptInterface() method. If you can live with targeting lower API levels (e.g. level 15) then changing the API target in AndroidManifest.xml to lower API levels will allow you to continue using this WebSocket library without modification.

vivekdpk commented 11 years ago

This issue happens if your target API is Jelly Beans MR1 or higher. If this is the case, the WebSocketFactory class should be updated to follow the new guidelines of the Android API about the usage of the addJavascriptInterface() method.

if i want to work with api 17. then in that case what should i have to do? how will i update WebSocketFactory class . Or may be some other trick or idea?

naoufalmed commented 11 years ago

I didn't try it myself, as I don't need to work with the newest APIs, but maybe you might find some guidelines in the Android API at: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object,%20java.lang.String) I hope this can help!