URL没有在webview中加载但在android中的浏览器中加载?

时间:2014-03-01 11:17:59

标签: javascript android url webview

某些类型的网址未在我的应用程序中加载,但可以在设备浏览器中加载。以下是我的代码中没有工作的Url示例 - “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;
    }
}

2 个答案:

答案 0 :(得分:1)

创建WebViewClient并加载url,如

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

setWebViewClient喜欢:

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

并在INTERNET

中添加manifest.xml权限
<uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:0)

尝试此解决方案。

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 );

    }

}