我有一个网页加载到我的 WebView
中。该网页加载网站的登录页面。当用户输入他的凭据时,他会被重定向到某个页面。我想在用户输入他的凭据并重定向到另一个 Activity 而不是默认的重定向页面后停止加载网页。
我这样做的想法是捕获重定向的 url 并将其与我创建的 url 进行比较,如果两者匹配,它应该停止加载默认网页并加载另一个 Activity 。但是,我能够捕获重定向的 Urls 并将它们显示在 AlertDialog
中,但无法停止默认网页的加载。
谁能帮我解决这个问题?
这是我试过的:-
myWebView.loadUrl(url);
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(progressBar.isShowing())
{
progressBar.dismiss();
}
String absoluteUrl = view.getUrl();
absoluteUrl = Uri.decode(absoluteUrl);
//int absoulteCount = absoluteUrl.length();
String redirectedUrl = endpointHost+"Authorize/index"+myId;
//int redirectedCount = redirectedUrl.length();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Details.this);
alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.setMessage(absoluteUrl);
alert.show();
if(absoluteUrl!=null && absoluteUrl.contains(redirectedUrl))
{
view.stopLoading();
Intent myIntent = new Intent(Details.this, Home.class);
startActivity(myIntent);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
view.loadUrl(url);
HttpClient httpClient = new DefaultHttpClient();
URL myUrl;
URLConnection connection;
try
{
myUrl = new URL(url);
connection = myUrl.openConnection();
connection.setConnectTimeout(3000);
connection.connect();
int size = connection.getContentLength();
}
catch (Exception e) {}
String htmlContent = "";
HttpPost httpGet = new HttpPost(url);
HttpResponse response;
HttpContext httpContext = new BasicHttpContext();
try
{
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = entity.getContent();
htmlContent = convertToString(inputStream);
}
}
}
catch (Exception e) {}
return true;
}
编辑:- 由@SimplePlan 建议
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(progressBar.isShowing())
{
progressBar.dismiss();
}
}
private void showAlert2(String Url)
{
// TODO Auto-generated method stub
Url = Uri.decode(Url);
//int absoulteCount = absoluteUrl.length();
String redirectedUrl = endpointHost+"/AuthorizeDevice/index"+deviceId;
//int redirectedCount = redirectedUrl.length();
if(Url!=null && Url.contains("redirect_uri"+redirectedUrl+"?{StandardTokens}"))
{
myWebView.stopLoading();
Intent myIntent = new Intent(Details.this, Home.class);
startActivity(myIntent);
}else{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Details.this);
alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.setMessage(Url);
alert.show();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
view.loadUrl(url);
showAlert2(url);
return true;
}
最佳答案
尝试像这样实现:
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(progressBar.isShowing())
{
progressBar.dismiss();
}
showAlert2(url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// TODO Auto-generated method stub
view.loadUrl(url);
HttpClient httpClient = new DefaultHttpClient();
URL myUrl;
URLConnection connection;
try
{
myUrl = new URL(url);
connection = myUrl.openConnection();
connection.setConnectTimeout(3000);
connection.connect();
int size = connection.getContentLength();
}
catch (Exception e) {}
String htmlContent = "";
HttpPost httpGet = new HttpPost(url);
HttpResponse response;
HttpContext httpContext = new BasicHttpContext();
try
{
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = entity.getContent();
htmlContent = convertToString(inputStream);
}
}
}
catch (Exception e) {}
return true;
}
和showAlert2(url)
方法:
public void showAlert2(String Url) {
//String absoluteUrl = view.getUrl();
Url = Uri.decode(Url);
//int absoulteCount = absoluteUrl.length();
String redirectedUrl = endpointHost+"Authorize/index"+myId;
//int redirectedCount = redirectedUrl.length();
if(Url!=null && Url.contains(redirectedUrl))
{
myWebView.stopLoading();
Intent myIntent = new Intent(Details.this, Home.class);
startActivity(myIntent);
}else{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Details.this);
alertDialogBuilder.setNegativeButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.setMessage(Url);
alert.show();
}
}
按照我的回答尝试并给我反馈
关于javascript - 停止在 Url 重定向 android 上加载网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22316655/