使用带有基本授权的WebView打开pdf文件

时间:2017-11-27 07:47:47

标签: android android-fragments webview http-basic-authentication

  

这是我使用WebView和BasicAuthentication

获取pdf文件的完整代码
public class ShowPdf extends Fragment {
private static final String TAG = "ShowPdf: ";
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
String pdf = "";
private final String HOST = Cons.BASE_URL;
private final String URL = "http://" + HOST + "/";
private final String USERNAME = Cons.USERNAME;
private final String PASSWORD = Cons.PASSWORD;
HashMap<String, String> map = new HashMap<String, String>();
WebView webview;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.show_pdf, container, false);
    sharedPreferences = getActivity().getSharedPreferences(Cons.MyPREF, Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();
    webview = (WebView) view.findViewById(R.id.web_view);
    webview.getSettings().setJavaScriptEnabled(true);
    Bundle bundle= this.getArguments();
    if (bundle.containsKey("pdf_url")){
        pdf= bundle.getString("pdf_url");
    }
    setBasicAuth();
    webview.loadUrl(pdf);

    return view;

}
protected void setBasicAuth() {
    WebViewDatabase.getInstance(getActivity()).clearHttpAuthUsernamePassword();
    webview.setHttpAuthUsernamePassword(HOST, HOST, USERNAME, PASSWORD);
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
            String[] up = view.getHttpAuthUsernamePassword(host, realm);
            handler.proceed(sharedPreferences.getString(Cons.USERNAME, ""), sharedPreferences.getString(Cons.PASSWORD, ""));
            if (up != null && up.length == 2) {
                handler.proceed(up[0], up[1]);
            }
            else {
                Log.e(TAG, "Could not find username/password for domain: " + host + ", with realm = " + realm);
            }
        }
    });
}

}

  

现在你可以解释我在这里做错了什么。并且Log不在Logcat中打印。

     

它是logcat数据

E/SysUtils: ApplicationContext is null in ApplicationStatus

E / libEGL:validate_display:255错误3008(EGL_BAD_DISPLAY) E / libEGL:validate_display:255错误3008(EGL_BAD_DISPLAY) E / eglCodecCommon:glUtilsParamSize:unknow param 0x00008d57 E / DataReductionProxySettingListener:由于异常而没有DRP密钥:java.lang.ClassNotFoundException:com.android.webview.chromium.Drp

3 个答案:

答案 0 :(得分:1)

WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "pdf_url_path";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

尝试使用像这样的

答案 1 :(得分:0)

为代码添加给定的功能......

protected void setBasicAuth() {
        WebViewDatabase.getInstance(this).clearHttpAuthUsernamePassword();
        webview.setHttpAuthUsernamePassword(HOST, HOST, USERNAME, PASSWORD);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
                String[] up = view.getHttpAuthUsernamePassword(host, realm);
                handler.proceed(USERNAME, PASSWORD);
                if (up != null && up.length == 2) {
                    handler.proceed(up[0], up[1]);
                }
                else {
                    Log.d("", "Could not find username/password for domain: " + host + ", with realm = " + realm);
                }
            }
        });
    }

并在webview.loadUrl(URL);

之前调用它

某些字段是......

private final String HOST = "basic-auth-sample.com";
private final String URL = "http://" + HOST + "/";
private final String USERNAME = "user";
private final String PASSWORD = "pass";

如果有任何疑问,请告诉我......

快乐的编码。

答案 2 :(得分:0)

webview.loadUrl(&#34; http://drive.google.com/viewerng/viewer?embedded=true&url=&#34; + yoururl);