android - Webview - 在显示网站之前更改页面源?

标签 android webview webviewclient

我有一个网站。在网站上,有一个文本“Pic”。当我第一次在 Webview 中显示网站时,我可以将文本更改为“PicGreat”。这行得通!

但是,稍后当用户单击链接(网站上的某处)时,我会将用户转发到新的 HTML 网站。在展示网站之前,我想将文本“Pic”更改为“PicGreat”。

我该怎么做?我应该写一个函数,然后在 “公共(public) bool 值 shouldOverrideUrlLoading”?

我在 Stackoverflow 上发现了一个类似的问题,但没有解决。 how to set webview client

主.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/webview"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
/>

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

WebTest.java

public class WebTestActivity extends Activity {

 WebView mWebView;
 String rline = "";

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.main);

 mWebView = (WebView) findViewById(R.id.webview);
 mWebView.getSettings().setJavaScriptEnabled(true);

 HttpURLConnection urlConnection = null;
 try
 {
 URL url = new URL("http://www.picSite.com/");
 urlConnection = (HttpURLConnection) url.openConnection();
 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
 BufferedReader rd = new BufferedReader(new InputStreamReader(in), 4096);
 String line;

 while ((line = rd.readLine()) != null) {
 rline += line+"\n";
 }
 rd.close();

 } catch (MalformedURLException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 if ( null != urlConnection )
 {
 urlConnection.disconnect();
 }
 }

 String getNewCode = rline.replace("Pic", "PicGreat");

 mWebView.loadData(getNewCode, "text/html", "utf-8");

 mWebView.setWebViewClient(new HelloWebViewClient());
 }

 private class HelloWebViewClient extends WebViewClient {
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {
 view.loadUrl(url);
 return true;
 }

 }
}

最佳答案

我会使用 WebViewClient.shouldInterceptRequest() :

Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used.

NOTE: This method is called on a thread other than the UI thread so clients should exercise caution when accessing private data or the view system.

关于android - Webview - 在显示网站之前更改页面源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9590467/

相关文章:

android - OpenGL ES 2.0 顶点错误垃圾收集器 (Android)

android - 启动android应用程序时出错(钛框架)

android - 如何在 Android RecyclerView 和 LayoutManager 中使用 scrollVerticallyBy()?

android - Webview 不加载 URL,但浏览器加载

安卓 : WebView onPageFinished not calling after shouldOverrideUrlLoading method in android 4. 4

javascript - 在 webview 中启用 Javascript 时,Android Webview 未检测到链接单击

android - 检查来自外部类的android连接/获取系统服务

android - 如何在 Android 中显示具有 Theme.Dialog 样式的 WebView

java - 在 Fragment 内使用 onProgressChanged 时出现空指针异常

android - 从 webview INPUT 字段上传相机照片和文件选择器