无法加载 Webview(已添加权限)
Not able to load Webview (permissions added)
我正在尝试创建一个将 url 加载到网络视图中的简单应用程序。但是,我不断收到 net::ERR_CACHE_MISS
。我已经添加了互联网的相关权限,这似乎是导致错误的最常见原因。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private static String TAG = "Main";
@Override
protected void onCreate(Bundle savedInstanceState) {
// getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
WebView webview = (WebView)findViewById(R.id.web_view);
webview.setWebViewClient(new myWebviewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://www.foodport.co.in");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class myWebviewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.i(TAG, "onReceivedError "+ errorCode);
}
}
我已将 <uses-permission android:name="ANDROID.PERMISSION.INTERNET"/>
添加到清单文件中。
有什么帮助吗?我唯一的线索是有另一个应用程序在同一个 phone 上使用相同的包名称,但也有所有权限。
我正在检查运行 Android 4.4 的 phone,我检查了 this post 但我不认为这是问题所在。
您必须设置 WebView webview = (WebView)findViewById(R.id.web_view);
在 OnCreate 方法中 而不是 onStart
在为 activity setContentView(R.layout.activity_layout);
设置布局后
试试这个代码
if (Build.VERSION.SDK_INT >= 19) {
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
or use this code
webview.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
I've added to the manifest file
Android 区分大小写。请将其编辑为 android.permission.INTERNET
.
我正在尝试创建一个将 url 加载到网络视图中的简单应用程序。但是,我不断收到 net::ERR_CACHE_MISS
。我已经添加了互联网的相关权限,这似乎是导致错误的最常见原因。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private static String TAG = "Main";
@Override
protected void onCreate(Bundle savedInstanceState) {
// getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
WebView webview = (WebView)findViewById(R.id.web_view);
webview.setWebViewClient(new myWebviewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://www.foodport.co.in");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class myWebviewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.i(TAG, "onReceivedError "+ errorCode);
}
}
我已将 <uses-permission android:name="ANDROID.PERMISSION.INTERNET"/>
添加到清单文件中。
有什么帮助吗?我唯一的线索是有另一个应用程序在同一个 phone 上使用相同的包名称,但也有所有权限。 我正在检查运行 Android 4.4 的 phone,我检查了 this post 但我不认为这是问题所在。
您必须设置 WebView webview = (WebView)findViewById(R.id.web_view);
在 OnCreate 方法中 而不是 onStart
在为 activity setContentView(R.layout.activity_layout);
试试这个代码
if (Build.VERSION.SDK_INT >= 19) {
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
or use this code
webview.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
I've added to the manifest file
Android 区分大小写。请将其编辑为 android.permission.INTERNET
.