user3004033
user3004033

Reputation: 11

read pdf file from server

I am trying to develop android app that can view pdf file from the server. I have edited my code. Check it out.

Here the code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

                //String LinkToPDF = "http://127.0.0.1/joomla/images/appletter.pdf";
                WebView mWebView=new WebView(MainActivity.this);
                mWebView.getSettings().setJavaScriptEnabled(true);
                mWebView.getSettings().setPluginsEnabled(true);
                mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url=nizam.hostingsiteforfree.com/do‌​c_8.pdf");
                setContentView(mWebView);

            mWebView.setWebViewClient(new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return false;
                    }
                });
              }

There's no error with that. But it displays this whenever i click the button "read pdf"

"Sorry we were unable to find the document at the original source. Verify that the document still exists."

Please help me get rid of this problem. Any help?

Upvotes: 1

Views: 1192

Answers (1)

Nizam
Nizam

Reputation: 5731

While referring localhost from Emulator, use http://10.0.2.2/ instead of your local ip.
So the change in your code will be

String LinkToPDF = "http://10.0.2.2/joomla/images/appletter.pdf";

Also, this will work only with Emulator. For real device, you need real server.

UPDATE:
I tested your code. Its working only for a real server. Seems like 'google docs' can't access localhost files. So, test using a real server. There are many free hosting sites which you can use for testing purpose.

Joomla is not a server. Joomla is a content management system (CMS). You installed Joomla in your 'localhost', which is only an imitation of server, not a real server.(one of the free hosting site- freeHosting). Register on real server(can use free hosting for now), get your own url, and upload pdf to there. Feel free to mail me if you need more details about hosting.

Now, an important part:- SSL certificate. Free hostings may not have a valid SSL. So, refer as following:

    String LinkToPDF = "http://nizam.hostingsiteforfree.com/doc_8.pdf";
    WebView webview=new WebView(MainActivity.this);
    webview.getSettings().setJavaScriptEnabled(true);

    final Activity activity = this;
    webview.setWebViewClient(new WebViewClient() {
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
      }
      @Override
        public void onReceivedSslError(WebView view,
                SslErrorHandler handler, SslError error) {
            Log.d("Nzm", ""+error.getPrimaryError());
             Toast.makeText(activity, "Skipping SSL error", Toast.LENGTH_SHORT).show();
            handler.proceed();
        }
    });

    setContentView(webview);
    webview.loadUrl("https://docs.google.com/gview?embedded=true&url=nizam.hostingsiteforfree.com/doc_8.pdf");

Upvotes: 1

Related Questions