如何在Android浏览器中将html文档作为字符串

时间:2016-08-30 09:22:03

标签: javascript android html dom html-parsing

在我的代码中。我想在加载网页时获取生成的源代码(html文档)。函数onPageFinished()我写了view.loadUrl(“javascript:(function(){document.getElementByTagName('html')[0] .innerHTML”);用于获取代码,但它显示错误“[INFO:CONSOLE (1)]“Uncaught SyntaxError:意外的输入结束”,来源:(1)“。 网页显示正确 我想获取源代码网页。检索源代码并显示它的方法是什么?怎么可能?

我的代码如下所示:

 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.view.KeyEvent;
 import android.view.View;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ProgressBar;



 public class MainActivity extends Activity {

WebView web1;
EditText ed1;
Button bt1;
String Address;
String add;
ProgressBar pbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    web1 = (WebView)findViewById(R.id.webView1);
    ed1 = (EditText)findViewById(R.id.editText1);
    bt1 = (Button)findViewById(R.id.button1);
    pbar = (ProgressBar)findViewById(R.id.progressBar1);
    pbar.setVisibility(View.GONE);

    bt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Address = "http://" + ed1.getText().toString();
            WebSettings webSetting = web1.getSettings();
            webSetting.setBuiltInZoomControls(true);
            webSetting.setJavaScriptEnabled(true);
            webSetting.setDomStorageEnabled(true);

            web1.setWebViewClient(new WebViewClient());

            web1.loadUrl(Address);

        }
    });
}

public class WebViewClient extends android.webkit.WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {

        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
        pbar.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        // TODO Auto-generated method stub
        return true;
    }
    @Override
    public void onPageFinished(WebView view, String url) {

        // TODO Auto-generated method stub

        super.onPageFinished(view, url);
        pbar.setVisibility(View.GONE);
        view.loadUrl("javascript:(function() { document.getElementByTagName('html')[0].innerHTML");
    }

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
        web1.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 }

1 个答案:

答案 0 :(得分:0)

您可以从这样的网页获取文字,然后按照您的意愿执行:

public class HTMLPageDownloader extends AsyncTask<Void, Void, String> {
        public static interface HTMLPageDownloaderListener {
            public abstract void completionCallBack(String html);
        }
        public HTMLPageDownloaderListener listener;
        public String link;
        public HTMLPageDownloader (String aLink, HTMLPageDownloaderListener aListener) {
            listener = aListener;
            link = aLink;
        }

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(link);
            String html = "";
            try {
                HttpResponse response = client.execute(request);
                InputStream in;
                in = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(in));
                StringBuilder str = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
                in.close();
                html = str.toString();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return html;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (!isCancelled()) {
                listener.completionCallBack(result);
            }
        }
    }