我正在尝试使用webView在Android应用中显示一个URL。
如果我尝试使用此网址 -
但如果我正在尝试这个 -
我得到了这个 -
我所做的是 -
活动 -
package com.appnucleus.abrarjahin.hello8920;
import android.content.Context;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
public class ActivityMain extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( isNetworkAvailable() ) //check if internet available or not
{
Toast.makeText(
ActivityMain.this,
"Internet Connected",
Toast.LENGTH_LONG
).show();
WebView webview = (WebView)findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(getString(R.string.sound_cloud_url));
}
else //Not connected
{
Toast.makeText(
ActivityMain.this,
"Internet Disconnected",
Toast.LENGTH_LONG
).show();
}
}
//<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//<uses-permission android:name="android.permission.INTERNET"/>
public boolean isNetworkAvailable()
{
final ConnectivityManager connectivityManager = ((ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
}
布局 -
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.appnucleus.abrarjahin.hello8920.ActivityMain">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
的strings.xml -
<resources>
<string name="app_name" translatable="false">Hello 8920</string>
<string name="sound_cloud_url">https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F8111706%3Fsecret_token%3Ds-3aK27&color=e2ef3a&auto_play=false&show_artwork=false</string>
<string name="test_url">http://stackoverflow.com/</string>
</resources>
的AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appnucleus.abrarjahin.hello8920">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ActivityMain">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
任何人都可以帮忙,为什么我得到一个空白页?
答案 0 :(得分:2)
试试这个,
webView = (WebView) findViewById(R.id.webView);
**webView.setWebViewClient(new WebViewClient());**
String url = "https://www.google.co.in";
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
这可能对你有所帮助。
答案 1 :(得分:0)
在 WebView 上添加WebViewClient
并检查您获得的错误。
如何设定?
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.e("Error", description);
}
});
希望这是有道理的。
答案 2 :(得分:0)
试用此代码: -
public class ActivityMain extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( isNetworkAvailable() ) //check if internet available or not
{
Toast.makeText(
ActivityMain.this,
"Internet Connected",
Toast.LENGTH_LONG
).show();
WebView webview = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(getString(R.string.sound_cloud_url));
}
else //Not connected
{
Toast.makeText(
ActivityMain.this,
"Internet Disconnected",
Toast.LENGTH_LONG
).show();
}
}
//<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//<uses-permission android:name="android.permission.INTERNET"/>
public boolean isNetworkAvailable()
{
final ConnectivityManager connectivityManager = ((ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
}
答案 3 :(得分:0)
您可以覆盖onReceivedSslError
中的webViewClient
。
aboutALCWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.d("Ssl Error:",handler.toString() + "error:" + error);
handler.proceed();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
答案 4 :(得分:-3)
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);