1

I developed a small android webview app to access an internal (local network) PHP based site.

Here is my code:

package com.CheckInventory;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressWarnings("unused")
public class CheckInventoryActivity extends Activity {
    WebView webview;
    String username; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.setBackgroundColor(0);
        webView.setBackgroundResource(R.drawable.myimage);
        webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        WebSettings webSettings = webview.getSettings();
        webSettings.setLoadWithOverviewMode(true);

        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
        webSettings.setDomStorageEnabled(true);
        webview.setWebChromeClient(new WebChromeClient());
        webview.loadUrl("http://192.168.0.124/android");
        webview.setWebViewClient(new HelloWebViewClient());

    }
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event) {
        if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
            //webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

The site has authentication, but I would like to add some authentication between the app and the site (How do I do this? pass a parameter maybe when the url is invoked), secondly and more importantly, where specifically do I put the onReceivedError so the user never sees the url or the page down if they walk away from the building or loose connection.

public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
        Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
    }

I saw this explanation in Detecting Webview Error and Show Message but I do not know where to implement it.

Thank you in advance

Community
  • 1
  • 1
MrM
  • 85
  • 1
  • 3
  • 12

2 Answers2

2

You need to create class which extends WebViewClient and implements method OnReceivedError

like this

class myWebClient extends WebViewClient {

    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
        Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

}

and then you need to set new WebViewClient to your WebView

    web = (WebView) findViewById(R.id.webview);
    web.setWebViewClient(new myWebClient());

Hope this helps

UnQuaiz
  • 23
  • 3
  • UnQuaiz: I am not sure how to implement your answer, can you show me the entire code as you would code it? – MrM Dec 19 '12 at 20:44
  • I sincerely want to thank you for your time!!! Unfortunately, it did not work. It still showed the not found and the url. Ultimately, I am looking for a solution similar to the google play where it shows a retry button if the internet is not available. Since it is being done by the guys at google, it is possible, I simple wonder how difficult this is. – MrM Dec 20 '12 at 21:07
  • Maybe you should check for internet connection first of all, and then show warning and retry button? Try this : http://stackoverflow.com/questions/11096987/check-real-internet-connection – UnQuaiz Dec 21 '12 at 03:12
  • UnQuaiz:package com.GlobalChess; import android.widget.Toast; import android.content.Context; public class JavaScriptInterface { Context mContext; /** Instantiate the interface and set the context */ JavaScriptInterface(Context c) { mContext = c; } /** Show a toast from the web page */ public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show(); } } – MrM Dec 21 '12 at 22:07
1

You can add onReceivedError in Your HelloWebViewClient class and handle what you want to handle when you get error.

private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
           //Your code to do
        Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
        }
    }
Upendra Shah
  • 2,218
  • 17
  • 27