2016-10-28 82 views
1

我正在一個應用程序中,我想顯示我的網站,但少div元素。Android WebView Jsoup

在此示例代碼中,我使用了stackoverflow.com的網站,並試圖隱藏div類hmenus(主網站頂部的按鈕)。

運行代碼後,我得到一個空白/白色的web視圖。我所嘗試的是製作String html final,然後調用它來設置webview的最終url。

代碼:

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.select.Elements; 
import java.io.File; 

public class MyCustomAsyncTask extends AsyncTask<File, Void, String> { 
@Override 
protected String doInBackground(File... params) { 

    Document doc = Jsoup.connect("http://stackoverflow.com/").get(); 
    Elements ele = doc.select(":not(#hmenus)");  // ->>>>> Jsoup html 
    final String html = ele.toString(); 

}} 

public class MainActivity extends Activity { 


    private WebView WebView; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     WebView = (WebView) findViewById(webView); 
     WebView.setWebChromeClient(new WebChromeClient()); 
     WebView.getSettings().setJavaScriptEnabled(true); 
     WebView.getSettings().setDomStorageEnabled(true); 

     final String mime = "text/html"; 
     final String encoding = "utf-8"; 
     WebView.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onPageFinished(WebView view, String url) { 

       // ->>>>>> Change the website --->>> insert call html value 
       WebView.loadData(html, mime, encoding); 
      } 
     }); 
    } 
} 

我如何可以調用HTML final String html = ele.toString();的值,並將其轉換爲WebView.loadData(html, mime, encoding);

回答

0

您沒有正確使用AsyncTask。您應該閱讀this

聲明AsyncTask貫徹doInBackground用於獲取網頁和onPostExecute顯示的結果時,它已準備就緒:

private class GetData extends AsyncTask<Void, Void, String> { 

// This is run in a background thread 
    @Override 
    protected String doInBackground(Void... params) { 
     try { 
      Document doc = Jsoup.connect("https://stackoverflow.com/") 
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0") 
        .get(); 
      Elements ele = doc.select("#hmenus"); 
      html = ele.toString(); 
      return html; 
     } catch (Exception e) { 
      Log.d("APP", e.toString()); 
     }    
     return "error"; 
    } 

    // This runs in UI when background thread finishes 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     Log.d("APP", "1"); 
     WebView.loadData(result, mime, encoding);    
    } 
} 

至於MainActivity,它必須調用AsyuncTask

public class MainActivity extends Activity { 

    private WebView WebView; 
    String html = "Loading..."; 
    final String mime = "text/html"; 
    final String encoding = "utf-8"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     WebView = (WebView) findViewById(R.id.webview); 
     WebView.setWebChromeClient(new WebChromeClient()); 
     WebView.getSettings().setJavaScriptEnabled(true); 
     WebView.getSettings().setDomStorageEnabled(true); 
     //Display "Loading..." message while waiting 
     WebView.loadData(html, mime, encoding); 
     //Invoke the AsyncTask 
     new GetData().execute(); 
    } 
} 

不要忘記你清單上的internet-permission

+0

我已經對您建議的代碼進行了修改,聽起來很明顯。但它不能解決我最初的問題 - 這是調用實際的html值設置 –

+0

我已經實現了您的建議更改。已更新我的第一篇文章。我仍然在那個地方,我沒有得到 - 最終的String html = ele.toString(); - 在我的WebView.loadData(html,mime,encoding); –

+0

你可以編輯你的問題並添加你現在使用的代碼嗎? – TDG