2013-02-01 4 views
0

Je travaille avec Android WebView mon objectif est d'analyser html avant de rendreObtenir Html de WebView avant de rendre

pour ce faire, j'ai ajouté suivant javascript pour WebView dans l'événement pagefinish ..

public void onPageFinished(WebView view, String url) 
{ 
    view.loadUrl("javascript:varhtmlString=document.getElementsByTagName('html')[0].innerHTML;" 
       +"document.getElementsByTagName('html')[0].innerHTML=window.HTMLOUT.parseHTML(htmlString);");    
} 

mais problème est un retour en arrière (html original) apparaît avant d'analyser html

puis je surveille les journaux et a constaté que javascript est en cours d'exécution après pageFinish (asynchrone) pour le rendre synchrone j'ai utilisé attendre aviser mécanisme et veiller à ce que Javasript soit lancé avant la page finale

mais toujours le même problème html d'origine apparaît avant parse

est il y a un moyen de changer html avant de rendre ????

Répondre

1

Vous pouvez le faire de cette façon:

String content = getContentForUrl(url); 
String manipulateHTML = manipulate(content); // your function to adjust the content. 

webView.loadDataWithBaseURL(url, manipulateHTML, "text/html","UTF-8", null); 

public String getContentForUrl(String url) { 

    BufferedReader in = null; 

    String content = ""; 

    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(url); 
     HttpResponse response = client.execute(request); 

     in = new BufferedReader(new InputStreamReader(response.getEntity() 
       .getContent())); 

     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 

     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 

     in.close(); 
     content = sb.toString(); 

     Log.d("MyApp", "url content for " + url + " " + content); 

    } catch (Exception e) { 

     Log.d("MyApp", 
       "url content for " + url + " Exception :" + e.toString()); 

    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    return content; 
} 
+0

ce que sur les demandes de poste? – gondo

Questions connexes