4

I am working in android studio.I have used webview in main activity. When I run my project then it gives an error like... "The webpage at http://www.google.com could not be loaded because: net::ERR_CACHE_MISS". Please see my code: In manifest:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".defaultActivity"
            android:label="@string/app_name"
          >
            <uses-permission android:name="android.permission.INTERNET"/>
            <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
            <uses-permission android:name="android.permission.android.permission.ACCESS_NETWORK_STATE"/>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

In Activity:

WebView _taskOrganizerView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_default);
        _taskOrganizerView =   (WebView)findViewById(R.id.wvTskOrg);
        _taskOrganizerView.getSettings().setJavaScriptEnabled(true);
        if (Build.VERSION.SDK_INT >= 19) {
            _taskOrganizerView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
        _taskOrganizerView.loadUrl("http://www.google.com");
        _taskOrganizerView.setWebViewClient(new WebViewClient() );
    }

In Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".defaultActivity">

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/wvTskOrg"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>
e4c5
  • 52,766
  • 11
  • 101
  • 134
Amin Uddin
  • 968
  • 6
  • 15
  • 31

5 Answers5

6

I face the same issue today, and at last I found that I just forget to add permission annoncement in AndroidManifest.xml file. It just went fine after I add

<uses-permission android:name="android.permission.INTERNET"/>

in AndroidManifest.xml

bingbing
  • 91
  • 1
  • 5
2

Use The Internet Permission Outside the application tag in android manifest file.

Muzmail Hussain
  • 95
  • 2
  • 10
2

From the code above it looks like you are trying to set the permissions in the <application>-tag.

As @zehata in this answer states, permissions should be granted outside the <application>-tag in the manifest file. Put it right at the beginning since the place seems to be important too. Try something like this:

    <manifest package="your.awesome.package"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.android.permission.ACCESS_NETWORK_STATE"/>

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".defaultActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

        <!-- more content of manifest file -->

   </manifest>
KraffMann
  • 322
  • 1
  • 4
  • 14
1

You might be a victim of this bug:

Issue 424599: Failed to load resource: net::ERR_CACHE_MISS error when opening DevTools on PHP pages

It seems to occur with pages that have the Cache-Control response header

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • As I understand it, the default caching behavior of WebView is to use cache while respecting the caching policy of the web page. Not setting a cache mode at all will still use cache, but only when the page says it's okay. – kris larson Sep 22 '15 at 13:50
1

your code:

if (Build.VERSION.SDK_INT >= 19) {
            _taskOrganizerView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }

if you never load "http://www.google.com", you setCacheMode LOAD_CACHE_ELSE_NETWORK would be error,you can :

webview.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                //setWebView Gone
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            }

            @Override
            public void onPageFinished(WebView view, final String url) {
                super.onPageFinished(view, url);


            }
YaC King
  • 109
  • 1
  • 8