title: How to pass non-primitive Object from Java to JS by Android addJavascriptInterface?
body: <p>I've had a trouble on this topic (for days).</p>
  
  <p>For instance, basically, I want to pass a Java WebSocketClient object to Android Webview JS <strong>on demand</strong>, so wrote a code referring to:</p>
  
  <ul>
  <li><a href="http://foretribe.blogspot.com.au/2013/08/how-to-make-android-webview-support.html" rel="nofollow noreferrer">http://foretribe.blogspot.com.au/2013/08/how-to-make-android-webview-support.html</a></li>
  <li><a href="https://github.com/thinksource/vw_websocket/blob/master/src/com/strumsoft/websocket/phonegap/WebSocketFactory.java" rel="nofollow noreferrer">https://github.com/thinksource/vw_websocket/blob/master/src/com/strumsoft/websocket/phonegap/WebSocketFactory.java</a></li>
  </ul>
  
  <p><strong>JAVA</strong></p>
  
  <pre><code>    wv.addJavascriptInterface(new WebSocketFactory(), "factoryJ");
  
      public class WebSocketFactory {
              //.............
              public WebSocket getInstance(String url) { 
                 socket = new WebSocket(new URI(url)); 
                 return socket; 
              }   
      }
  </code></pre>
  
  <p><strong>JS</strong></p>
  
  <pre><code> var url = "ws:someaddress";
   var ws = factoryJ.getInstance(url);  
   console.log(ws.toString())  // object pointer displayed
   console.log(ws.getReadyState()); //Uncaught Error: Error calling method on NPObject! 
  </code></pre>
  
  <p><code>Uncaught Error: Error calling method on NPObject!</code></p>
  
  <p>This concept does not work at least for me avove Android4.2+.,
  because <code>addJavascriptInterface()</code> only works with Java primitive types and Strings.</p>
  
  <p>cf) </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2250917/passing-a-javascript-object-using-addjavascriptinterface-on-android">Passing a JavaScript object using addJavascriptInterface() on Android</a></li>
  <li><a href="https://stackoverflow.com/questions/3344256/error-calling-method-on-npobject-in-android-2-2">Error calling method on NPObject! in Android 2.2</a></li>
  </ul>
  
  <p><strong>As far as I know, the only way to pass JAVA object to JS is :</strong></p>
  
  <pre><code>wv.addJavascriptInterface(JavaObject, "JsObject");
  </code></pre>
  
  <p>Sure, this should work fine as long as a passing <code>JavaObject</code> is pre-determined, but since WebSocket Object(s) is on-demand, I need to hack somehow for this.</p>
  
  <p>So, I prepare <code>JavaObject</code> as some Array of WebSocket .</p>
  
  <pre><code>   WebSocketNew[] ws = new WebSocketNew[99999];
     wv.addJavascriptInterface(ws, "wsJ");
  </code></pre>
  
  <p>Unfortunately, the JS treats <code>wsJ[n]</code> as <code>undefined</code> ; appears it's also not allowed to pass ArrayObject.</p>
  
  <p>I've read <code>JSON</code> or <code>JSON array</code> of Java can be passed, but it's <code>string</code> after all and cannot be this solution, am I correct?</p>
  
  <p>Probably, back in old days, Android Java-JS interaction is implemented more freely, but under security issues, they restrict more (the annotation <code>@JavascriptInterface</code> is the one, and not only this but also other factors ) <a href="https://stackoverflow.com/questions/14031635/android-4-2-1-webview-and-javascript-interface-breaks">Android 4.2.1, WebView and javascript interface breaks</a></p>
  
  <p>How to pass non-primitive Object from Java to JS by Android addJavascriptInterface?</p>
  
  <p>Any thought?</p>
  
  <p>Thanks.</p>