URL not loading in webview but loaded in browser in android?

拜拜、爱过 提交于 2020-01-30 07:57:05

问题


Some type of Urls are not loading in my app in Webview but it can be loaded in device browser. Following is the example Url which is not working in my code- "http://apps.takeyourapp.com/testApp/staging/index.html"

package com.example.webviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

    WebView webViewPlaceholder;
    String URL = "http://apps.takeyourapp.com/testApp/staging/index.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webViewPlaceholder = (WebView) findViewById(R.id.webholder);
        webViewPlaceholder.getSettings().setJavaScriptEnabled(true);
        webViewPlaceholder
                .setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webViewPlaceholder.setScrollbarFadingEnabled(true);
        webViewPlaceholder.getSettings().setLoadsImagesAutomatically(true);
        webViewPlaceholder.getSettings().setUseWideViewPort(true);
        webViewPlaceholder.loadUrl(URL);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

回答1:


Create WebViewClient and load url like

 public class myWebClient extends WebViewClient {
    @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
}

And setWebViewClient like:

    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");

And also add INTERNET permission into manifest.xml

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



回答2:


Try this solution.

public class Main extends Activity {

    private WebView mWebview ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mWebview  = new WebView(this);

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
        setContentView(mWebview );

    }

}


来源:https://stackoverflow.com/questions/22113651/url-not-loading-in-webview-but-loaded-in-browser-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!