加载时在webview中显示白页(PDF url)url android
Posted
技术标签:
【中文标题】加载时在webview中显示白页(PDF url)url android【英文标题】:show white page in webview when load (PDF url) url android 【发布时间】:2020-07-21 19:15:15 【问题描述】:我尝试将 pdf URL 加载到 webview 中,但有时 URL 加载完美或有时显示白页。它显示一个白页,因为它直接调用onPageFinished
方法。我尝试登录onReceivedSslError
,onReceivedHttpError
,但是当加载白屏时它直接调用onPageFinished
方法。如何每次使用 URL 加载我的 pdf?
我的代码是:-
url = getIntent().getStringExtra("url");
fileName = getIntent().getStringExtra("name");
try
masterURL = "https://drive.google.com/viewerng/viewer?embedded=true&url=" + URLEncoder.encode(url, "UTF-8");
catch (UnsupportedEncodingException e)
e.printStackTrace();
requireNonNull(getSupportActionBar()).setTitle(fileName + " Report");
WebSettings settings = webview.getSettings();
settings.setjavascriptEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setDatabaseEnabled(true);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
//webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
//webview.invalidate();
webview.setWebViewClient(new WebViewClient()
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
Toast.makeText(TestPDFActivity.this, errorCode, Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
@Override
public void onPageFinished(WebView view, String url)
progressDialog.dismiss();
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
super.onReceivedSslError(view, handler, error);
handler.proceed();
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse)
super.onReceivedHttpError(view, request, errorResponse);
);
webview.loadUrl(masterURL);
【问题讨论】:
您是否在Manifest
文件中声明了internet
和access network state
权限
请查看***.com/a/61005267/7846071
@NensiKasundra 是的,我在清单中添加了
你检查上面的链接吗
不工作@NensiKasundra
【参考方案1】:
最后我得到了 ans 只需将此行添加到 onPageFinished 方法中:-
if (view.getTitle().equals(""))
view.reload();
并且繁荣它的工作!
【讨论】:
你真的救了我。谢谢!【参考方案2】:这是工作代码。这可能会有所帮助。
XML
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".activities.WebViewActivity">
<WebView
android:id="@+id/webview"
android:layout_
android:layout_ />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
科特林
class WebViewActivity : AppCompatActivity()
private lateinit var activityWebViewBinding: ActivityWebViewBinding
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
activityWebViewBinding = DataBindingUtil.setContentView(this, R.layout.activity_web_view)
val path = "https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf"
loadPdfFromURL(path)
@SuppressLint("SetJavaScriptEnabled")
private fun loadPdfFromURL(path: String?)
activityWebViewBinding.webview.settings.loadWithOverviewMode = true
activityWebViewBinding.webview.settings.javaScriptEnabled = true
val url = "https://docs.google.com/gview?embedded=true&url=$path"
activityWebViewBinding.webview.loadUrl(url)
【讨论】:
请检查我的网址是否有效【参考方案3】:将此行添加到 onPageFinished 方法中
if (view.getTitle().equals("")) view.reload();
【讨论】:
以上是关于加载时在webview中显示白页(PDF url)url android的主要内容,如果未能解决你的问题,请参考以下文章
使用 Swift 3 触摸时在 Webview 中获取 pdf 的实际坐标