显示在authendication Android的web视图PDF文件视图、文件、Android、authendication

2023-09-07 08:44:58 作者:带你回家(^_-)

我在Android应用程序中工作的我想在Android的WebView打开从链接的PDF文件。但链接已实施authendication。我不能,如果我把没有的PDF中的链接来打开pdf.But该链接,我能看到网页在android webview.So的authendication工作正常。问题是,当我追加 https://docs.google.com/gview?embedded =真放; URL = 的网址打开PDF。

请看看我的code。

 公共类MainActivity延伸活动{    @燮pressLint(SetJavaScriptEnabled)    @覆盖    保护无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的WebView的WebView =新的WebView(MainActivity.this);        webview.getSettings()setJavaScriptEnabled(真)。        字符串URL =                 https://docs.google.com/gview?embedded=true&url=+http://spahousingli.evero.com/SPADocuments/Forms/Clients/Admissions/ADM000000011.pdf;        webview.loadUrl(URL);        webview.setWebViewClient(新WebViewClient(){            公共无效onReceivedHttpAuthRequest(的WebView视图,                    android.webkit.HttpAuthHandler处理,字符串主机,                    字符串的境界){                handler.proceed(用户名,密码);            };        });        的setContentView(web视图);    }} 

解决方案

我觉得现在的问题是,用于(HTTP头中基本身份验证)记录的凭据发送到谷歌的网址,谷歌不会将其发送到spahousingli.evero.com。为此该spahousingli.evero.com没有收到认证头并拒绝从谷歌的请求。

我觉得首先下载PDF格式,并打开它使用Adobe Reader你的设备是最简单的解决方案。

编辑:结果下载PDF文件到您的手机。 (locationOfYou​​rPdfFile)使用以下code。打开pdf文件:

 文件pdfFile =新的文件(locationOfYou​​rPdfFile);URI路径= Uri.fromFile(pdfFile);意图pdfIntent =新意图(Intent.ACTION_VIEW);pdfIntent.setDataAndType(路径,应用程序/ PDF格式);pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);尝试{    startActivity(pdfIntent);}赶上(ActivityNotFoundException E){    Toast.makeText(MyPDFDemo.this,没有可用来查看PDF应用程序,Toast.LENGTH_LONG).show();    startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(https://play.google.com/store/apps/details?id=com.adobe.reader)));} 
android web mysql android通过web与后台数据库交互

I am working in an android application an I want to open a pdf from a link in an android webview. But the link has implemented authendication. I am not able to open that link with pdf.But if I place a link without the pdf, I am able to see the web page in the android webview.So the authendication works fine. The problem is when I append "https://docs.google.com/gview?embedded=true&url=" to the url to open the pdf.

Please look into my code.

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);

    }   
}

解决方案

I think that the problem is that the credentials used for logging in (http header for basic authentication) are sent to the google url and Google will not send them to spahousingli.evero.com. Therefor the spahousingli.evero.com does not receive the authentication header and will reject the request from Google.

I think downloading the pdf first and opening it with Adobe Reader on you device is the easiest solutions.

Edit: Download the pdf to your phone. (locationOfYourPdfFile) Open the pdf using the following code:

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")));
}