0
votes

I'm using WebViewClient to browse urls using the emulator. after a while (cannot tell exactly the cause), the pages cannot be loaded anymore or they view takes forever, for the same sites as before that were very fast. Usually i see under onReceivedError the error -2 (WebViewClient.ERROR_HOST_LOOKUP) Any help/reference would be appreciated.

Thanks!

W/ThrottleService( 91): unable to find stats for iface rmnet0
D/dalvikvm( 171): GC_CONCURRENT freed 457K, 8% free 6971K/7559K, paused 11ms+7ms
F/NetworkStats( 91): problem reading network stats
F/NetworkStats( 91): java.lang.IllegalStateException: problem parsing idx 1
F/NetworkStats( 91): at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
F/NetworkStats( 91): at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
F/NetworkStats( 91): at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
F/NetworkStats( 91): at com.android.server.net.NetworkStatsService.performPoll(NetworkStatsService.java:799)
F/NetworkStats( 91): at com.android.server.net.NetworkStatsService.access$100(NetworkStatsService.java:128)
F/NetworkStats( 91): at com.android.server.net.NetworkStatsService$3.onReceive(NetworkStatsService.java:633)
F/NetworkStats( 91): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
F/NetworkStats( 91): at android.os.Handler.handleCallback(Handler.java:605)
F/NetworkStats( 91): at android.os.Handler.dispatchMessage(Handler.java:92)
F/NetworkStats( 91): at android.os.Looper.loop(Looper.java:137)
F/NetworkStats( 91): at android.os.HandlerThread.run(HandlerThread.java:60)
F/NetworkStats( 91): Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
F/NetworkStats( 91): at libcore.io.IoBridge.open(IoBridge.java:406)
F/NetworkStats( 91): at java.io.FileInputStream.(FileInputStream.java:78)
F/NetworkStats( 91): at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
F/NetworkStats( 91): ... 10 more
F/NetworkStats( 91): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
F/NetworkStats( 91): at libcore.io.Posix.open(Native Method)
F/NetworkStats( 91): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
F/NetworkStats( 91): at libcore.io.IoBridge.open(IoBridge.java:390)
F/NetworkStats( 91): ... 12 more
D/dalvikvm( 91): GC_CONCURRENT freed 508K, 8% free 9025K/9735K, paused 6ms+24ms
W/ThrottleService( 91): unable to find stats for iface rmnet0

1
i just tried to load http://abc.com . it looks like the webview cannot load it. Can someone test it? Thanks - fredy

1 Answers

0
votes

Use below working code ::

public class Android_Activity extends Activity {
private Android_Activity _activity;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);     
            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            _activity = this;   
            setContentView(R.layout.main);

            mwebview=(WebView)view.findViewById(R.id.webview);
            mwebview.getSettings().setJavaScriptEnabled(true);
            mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

            if(checkInternetConnection(_activity)==true){
                if(savedInstanceState==null)
                    mwebview.loadUrl("http://abc.com");
                else
                    mwebview.restoreState(savedInstanceState);
            }
            else{
                AlertDialog.Builder builder = new AlertDialog.Builder(_activity);
                builder.setMessage("Please check your network connection.")
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {

                           }
                       });

                AlertDialog alert = builder.create();    
                alert.show();
            }
            mwebview.setWebChromeClient(new WebChromeClient() {

                @Override
                public void onProgressChanged(WebView view, int progress) { 
                    if(mwebview.getVisibility()==View.VISIBLE)
                    {
                        _activity.setProgress(progress * 100);
                    }
                }
            });
            mwebview.setWebViewClient(new HelloWebViewClient());
        }


        //HelloWebViewClient class for webview
        private class HelloWebViewClient extends WebViewClient {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }
            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);

            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
                view.loadUrl(url);
                return true;
            }

        }   //HelloWebViewClient-class
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // Check if the key event was the Back button and if there's history
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mwebview.canGoBack() ){
                mwebview.goBack();
                return true;
            }
            // If it wasn't the Back key or there's no web page history, bubble up to the default
            // system behavior (probably exit the activity)
            return super.onKeyDown(keyCode, event);
        }
        //To check whether network connection is available on device or not
            public static boolean checkInternetConnection(Activity _activity) {
                ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (conMgr.getActiveNetworkInfo() != null
                        && conMgr.getActiveNetworkInfo().isAvailable()
                        && conMgr.getActiveNetworkInfo().isConnected()) 
                    return true;
                else
                    return false;
            }//checkInternetConnection()
}

And also in your main.xml layout you must have a webview with id webview

Then replace the below lines and try

 if(savedInstanceState==null)
                    mwebview.loadUrl("http://abc.com");
                else
                    mwebview.restoreState(savedInstanceState);

with.. mwebview.loadUrl("http://abc.com");