Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am phasing a different problem when i am trying to load HTML String containing JavaScript code in to android WebView. If i save 1 html file in assets folder with same String in the file content and load in to WebView its working fine.

please find the code snippet i tried below.

 String MAP_STREETVIEW_DATA = "<html><head><meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" /><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /><script   src=\"http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA\"   type=\"text/javascript\"></script><script type=\"text/javascript\">     var map;    var directionsPanel;    var directions;var polyline;function initialize() {if (GBrowserIsCompatible()) {map = new GMap2(document.getElementById(\"map_canvas\"));directionsPanel = document.getElementById(\"route\");directions = new GDirections(map, directionsPanel);GEvent.addListener(directions, \"load\", directioninfo);GEvent.addListener(directions, \"error\", handleErrors);loadDirections();}}function handleErrors(){    Android.getResponse(\"error\",\"error\");       }function loadDirections() {var from=Android.getFromLatLong();var to=Android.getToLatLong();directions.load(\"from: \" + from + \" to: \" +to, {getPolyLine: true});}function directioninfo() {polyline = directions.getPolyline(); var count = polyline.getVertexCount();var i=0; var latlong=0;var descStep=\"\"; details.Android.getResponse(latlong,descStep);}</script></head><body style=\"margin: 0px; padding: 0px;\" onload=\"initialize()\"><div id=\"map_canvas\" style=\"width: 100%; height: 100%\"></div><div id=\"route\" style=\"width: 25%; height: 100%; float: right;\"></div></body></html>";

 String LOAD_URL_MAP = "file:/"+"/"+"/android_asset/direction.html"

 // Getting Geopoints by creating webview and loading script
  private void getPolyPoints() {
     try {
    webView = new WebView(ctContext);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

    if (Arrays.asList(getResources().getAssets().list("")).contains("direction.html")) {
                    **// Working Fine as Expected.**
        webView.loadUrl(LOAD_URL_MAP);
    } else {
                    **// Not Working In This Case**
        webView.loadData(MAP_STREETVIEW_DATA, "text/html", "UTF-8");
    }           

    } catch (Exception e) {

    }
}

Here is My JavaScriptInterface.java Class.

 public class JavaScriptInterface {
private MapUI mi;

public JavaScriptInterface(MapUI mapUI) {
    this.mi = mapUI;
}

public double getstviewLat() {// sending Startpoint to script
    return Double.parseDouble(wi.stlat);
}

public double getstviewLang() {// sending Startpoint to script
    return Double.parseDouble(wi.stlong);
}

public String getFromLatLong() {// sending Startpoint to script
    return mi.startPoint;
}

public String getToLatLong() {// sending endPoint to script
    return mi.endPoint;
}

/** Show a toast from the web page */
public void getResponse(String polypoints, String description) {
    // Getting geopoints and description from script
    if (!polypoints.equals("error")) {
        mi.latlong = polypoints;
        mi.routeDescption = description;
        mi.webView.destroy();
        mi.webView = null;
    }
    mi.settingGeopoints(polypoints);

}
}   

And Here is the HTML(direction.html) File in Assets Folder which is working fine.

  <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <!-- we need to genarate the JavaScript MapsAPI key and replace with the value of the key in the below script src url (This web Script needs Google Maps API key. A new key can be generated at http://code.google.com/apis/maps/documentation/javascript/v2/introduction.html#Obtaining_Key.)-->

    <script
src="http://maps.google.com/maps?file=api&v=2.x&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"
type="text/javascript"></script>
   <script type="text/javascript"> 

   var map;
   var directionsPanel;
   var directions;
   var polyline;

   function initialize() {

      if (GBrowserIsCompatible()) {
          map = new GMap2(document.getElementById("map_canvas"));

          directionsPanel = document.getElementById("route");
          directions = new GDirections(map, directionsPanel);
          GEvent.addListener(directions, "load", directioninfo);
          GEvent.addListener(directions, "error", handleErrors);
          loadDirections();
      }
    }

    function handleErrors(){
      Android.getResponse("error","error");
 }

    function loadDirections() {
          var from=Android.getFromLatLong();
          var to=Android.getToLatLong();
          directions.load("from: " + from + " to: " +to, {getPolyLine: true});
     }

    function directioninfo() {

          polyline = directions.getPolyline(); 

          var count = polyline.getVertexCount();

          var i=0;

          //poly line points
          var latlong=0;
          var descStep="";
          while (i < count) {
             var vertex = polyline.getVertex(i);
             var lat = vertex.lat();
             var lon = vertex.lng();
             latlong = latlong + " " + lat + "," + lon

             i++;
          }
         if (directions.getNumRoutes() > 0) {
             for (var i = 0; i < directions.getRoute(0).getNumSteps(); i++) {
                 descStep =descStep+"<br/>"+"&nbsp;"+directions.getRoute(0).getStep(i).getDescriptionHtml();
             }
          }

        //direction descriptions and other details.
        Android.getResponse(latlong,descStep);

   }
  </script>
  </head>
  <body style="margin: 0px; padding: 0px;" onload="initialize()">
      <div id="map_canvas" style="width: 100%; height: 100%"></div>
      <div id="route" style="width: 25%; height: 100%; float: right;"></div>
  </body>
  </html>

I am unable to debug this and unable check what's going wrong. I Have gone throu Google and unable to find any thing. So please help me if your able to find the issue in the process which i am trying to load HTML Content as String into WebView.

share|improve this question
    
When you say 'does not work', what do you mean? What happens? Crash, Blank page? Do you have a logcat output? –  etienne Apr 4 '13 at 8:09
    
I tested it; works fine on my side. Have you tried commenting out webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");? Did you add android.permission.INTERNET to your manifest file? –  etienne Apr 4 '13 at 8:25
    
I've added Internet permissions –  Raj Apr 4 '13 at 8:56
    
Its showing Blank screen with Exception : "Dynamiclayout(9300): java.lang.StringIndexOutOfBoundsException: length=0; index=0" But that Dynamic layout is not part of my coding. –  Raj Apr 4 '13 at 9:03
    
I am not sure that the error comes from the webview; have you tried commenting out other parts of your code to isolate the problem? –  etienne Apr 4 '13 at 9:06
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.