我正在使用android WebView
在我的应用中加载一些网址并且全部成功。但是当我试图加载这个网址时:
但由于某些原因我失败了 - " view.loadUrl(url);" in" shouldOverrideUrlLoading"不显示该页面,也不会加载其他网址。只是显示一个白页。 服务器的响应代码是304.
在我访问此网址之前,我正在使用Instagram进行身份验证并将访问令牌作为Cookie发送。
public class CloudWebViewActivity extends ActivityBase {
public static final String TAG = "WebViewActivity";
private static final String WEB_VIEW_TOKEN = "token";
private static final String WEB_VIEW_TITLE = "title";
private static final String WEB_VIEW_URL = "url";
private ProgressBar mPbProgress;
private Toolbar mToolbar;
private String mTitle, mUrl;
public static void setInstance(Context context, String title, String url) {
Intent starter = new Intent(context, CloudWebViewActivity.class);
starter.putExtra(WEB_VIEW_TITLE, title);
starter.putExtra(WEB_VIEW_URL, url);
context.startActivity(starter);
}
// MARK: Lifecycle
protected void onCreate(Bundle savedInstanceState) {
mTitle = getIntent().getStringExtra(WEB_VIEW_TITLE);
mUrl = getIntent().getStringExtra(WEB_VIEW_URL);
super.onCreate(savedInstanceState);
AppInstance.sharedInstance().getBus().register(this);
}
@Override
protected void onResume() {
super.onResume();
AppInstance.sharedInstance().setSelf();
mToolbar.setTitle(mTitle);
mPbProgress.setVisibility(View.VISIBLE);
}
@Override
protected void onDestroy() {
super.onDestroy();
AppInstance.sharedInstance().getBus().unregister(this);
}
@SuppressLint("SetJavaScriptEnabled")
public void findViews() {
setContentView(R.layout.webview_activity);
mPbProgress = (ProgressBar) findViewById(R.id.pBProgress);
final WebView mWebView = (WebView) findViewById(R.id.webView);
if (mWebView != null) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.setWebViewClient(webViewClient);
// Register a new JavaScript interface called HTMLOUT
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
}
if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly(mUrl) && mWebView != null) {
String cookieString = WEB_VIEW_TOKEN + "=" + NetworkManager.instance().getToken();
CookieManager.getInstance().setCookie(mUrl, cookieString);
mWebView.loadUrl(mUrl);
} else {
InfiLogger.getInstance().logRemoteException(new RuntimeException("Cloud service: " + mTitle + " came with an empty/null url address"));
AndroidUtils.showToast(R.string.error_cant_load_url);
}
}
@Override
public Toolbar setToolbar() {
mToolbar = (Toolbar) findViewById(R.id.mToolbar);
if (mToolbar != null) {
mToolbar.setVisibility(View.VISIBLE);
}
return mToolbar;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
private void handleProgressBarVisibility(String url) {
if (url.contains("login") ||
url.contains("https://api.twitter.com/oauth/authorize?oauth_token=") ||
url.contains("https://vimeo.com/log_in") ||
url.contains(".jpg")) {
mPbProgress.setVisibility(View.GONE);
} else {
mPbProgress.setVisibility(View.VISIBLE);
}
}
private final WebViewClient webViewClient = new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
handleProgressBarVisibility(url);
String cookieString = WEB_VIEW_TOKEN + "=" + NetworkManager.instance().getToken();
CookieManager.getInstance().setCookie(mUrl, cookieString);
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, final String url) {
if (url.startsWith("http://dev.infibond.com/api/cloud/")) {
mPbProgress.setVisibility(View.GONE);
// This call inject JavaScript into the page which just finished loading.
view.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AndroidUtils.showToast(error.getDescription());
} else {
AndroidUtils.showToast(R.string.gen_Something_went_wrong);
}
mPbProgress.setVisibility(View.INVISIBLE);
}
};
// An instance of this class will be registered as a JavaScript interface
class MyJavaScriptInterface {
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(String html) {
// process the html as needed by the app
InfiLogger.d("infi", "html: " + html);
if (html.contains("code")) {
final boolean success;
if (html.contains("\"code\":200")) {
AndroidUtils.showToast(AndroidUtils.getString(R.string.cloud_sync_start) + mTitle);
success = true;
} else {
AndroidUtils.showToast(AndroidUtils.getString(R.string.gen_Something_went_wrong));
success = false;
}
AppInstance.sharedInstance().getBus().post(new CloudConnectionStateChangedBusEvent(mTitle, success));
finish();
}
}
}
}
答案 0 :(得分:0)
我不是百分百肯定,但似乎你有很多小错误。我认为有问题的是:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
handleProgressBarVisibility(url);
String cookieString = WEB_VIEW_TOKEN + "=" +
NetworkManager.instance().getToken();
CookieManager.getInstance().setCookie(mUrl, cookieString);
view.loadUrl(url);
return true;
}
view.loadUrl(url)
对我来说似乎非常错误。我想您可能需要将其更改为:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("dev.infibond.com")) {
return false;
}
// otherwise, they are leaving the site, so open a new
// browser instead
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
并删除你在这里所做的内容。大多数是findViews()
,不管怎么说都不应该重复。我猜这就是你的意思
handleProgressBarVisibility(url)
WebViewClient
,但我不确定:
@Override
public void onLoadResource(WebView view, String url) {
handleProgressBarVisibility(url);
}