android - 在带有身份验证的android Webview中显示pdf

标签 android android-webview

我正在使用 android 应用程序,我想从 android webview 中的链接打开 pdf。但该链接已实现身份验证。我无法使用 pdf 打开该链接。但是如果我放置一个没有 pdf 的链接,我可以在 android webview 中看到该网页。因此身份验证工作正常。问题是当我将“https://docs.google.com/gview?embedded=true&url=”附加到网址以打开pdf时。

请查看我的代码。

public class MainActivity extends Activity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        WebView webview = new WebView(MainActivity.this);
        webview.getSettings().setJavaScriptEnabled(true);
        String url=
                 "https://docs.google.com/gview?embedded=true&url=" +"http://spahousingli.evero.com/SPADocuments/Forms/Clients/Admissions/ADM000000011.pdf";


        webview.loadUrl(url);
        webview.setWebViewClient(new WebViewClient() {
            public void onReceivedHttpAuthRequest(WebView view,
                    android.webkit.HttpAuthHandler handler, String host,
                    String realm) {

                handler.proceed("username", "password");
            };
        });
        setContentView(webview);

    }   
}

最佳答案

我认为问题在于用于登录的凭据(用于基本身份验证的 http header )被发送到 google url,而 Google 不会将它们发送到 spahousingli.evero.com。因此,spahousingli.evero.com 不会收到身份验证 header ,并将拒绝来自 Google 的请求。

我认为首先下载 pdf 并使用设备上的 Adob​​e Reader 打开它是最简单的解决方案。

编辑:
将 pdf 下载到您的手机上。 (您的 Pdf 文件位置) 使用以下代码打开 pdf:

File pdfFile = new File(locationOfYourPdfFile);
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path,"application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

try{
    startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
    Toast.makeText(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader")));
}

关于android - 在带有身份验证的android Webview中显示pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153246/

相关文章:

javascript - Android WebView 和执行 javascript

android - 升级后 Firebase Auth UI 出现 Google 登录错误

android - 如何防止黑客在 Android 上访问我的应用程序数据库?

android - 键盘向上移动底部选项卡按钮,并在 android 中隐藏 EditText 和 CheckBox

android - 需要在 HTML webview 中显示文本格式的数据,如 Stack Overflow

android - fragment 中的 WebView 不加载 HTML 字符串

java - HashMap 和 ArrayList 的 Android OutOfMemoryError

android - 应用关闭/终止时 BroadcastReceiver 无法显示通知

android-layout - WebView 方向更改问题

android - "block all network load requests"是什么意思?