【问题标题】:Show a pdf in android Webview with authendication在具有身份验证的android Webview中显示pdf
【发布时间】:2014-08-06 06:03:19
【问题描述】:

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

    }   
}

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

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

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

    编辑:
    将pdf下载到您的手机。 (locationOfYourPdfFile) 使用以下代码打开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")));
    }
    

    【讨论】:

    • 那么有什么解决方案吗?
    • 是的,下载文件并在本地使用 Adob​​e Reader 打开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多