这是一个从短网址获取长网址的功能,例如goo.gl,bit.ly ...
private String expandUrl(String shortUrl) {
String finalURL = "";
webView.loadUrl(shortUrl);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
finalURL = webView.getUrl();
}
});
return finalURL;
}
我想将webView.getUrl()
存储到字符串调用finalURL
,以便我以后可以使用它,但它会出错
错误:从内部类中访问局部变量finalURL;需要被宣布为最终
我的错误是什么?
答案 0 :(得分:0)
您正在匿名类中使用已定义的字段,如果您需要更改此字段,则将其设为类级变量。在所有方法之外声明它。 试试这个
public String finalURL = "";
答案 1 :(得分:0)
如果finalURL变量是外部类的成员变量,那么您可以将webView.getUrl()保存到finalURL以用于以后的目的,例如解析但如果finalURL是局部变量,那么除非你是不可能的将它作为数组对象和最终变量。 使用以下代码。而不是使用普通的finalURL,使用finalURL [0]来存储和解析。
final String[] finalURL = {""};
String url1 = "goo.gl/RvoAZH";
webView.loadUrl(url1);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
System.out.println(webView.getUrl()); // whatever way to debug
finalURL[0] = webView.getUrl();
}
});
在下面的示例中,您可以看到局部变量localVar的值被来自""的匿名对象内的方法(anonyMethod)更改。到了
public class Check {
public void method() {
final String[] localVar = {""};
new Object() {
public void anonyMethod() {
System.out.println(localVar[0]); // sure
localVar[0] = "a"; // nope
}
}.anonyMethod();
System.out.println(localVar[0]);
}
public static void main(String[] args)
{
Check ck = new Check();
ck.method();
}
}
编辑1 您还可以发布更多代码。
编辑2 添加了我自己的示例代码。
答案 2 :(得分:0)
你可以尝试这个
private class MyWebViewClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(Uri.decode(url));
return handleUri(uri);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(uri);
}
private boolean handleUri(final Uri uri) {
return false;
}
@Override
public void onLoadResource(WebView view, String url) {
if (url.equals("http://yoururl.com")) {
// do something
}
}
@Override
public void onFormResubmission(WebView view, Message dontResend, Message resend) {
resend.sendToTarget();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mCacheWebView.setVisibility(View.INVISIBLE);
showSwipeRefresh();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (webViewError) {
setErrorWebView();
} else {
if (mCacheWebView != null) {
mCacheWebView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('header')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
mCacheWebView.setVisibility(View.VISIBLE);
}
}
hideSwipeRefresh();
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
String message = "SSL Certificate error.";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message = "The certificate authority is not trusted.";
break;
case SslError.SSL_EXPIRED:
message = "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message = "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message = "The certificate is not yet valid.";
break;
}
message += " Do you want to continue anyway?";
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
onReceivedError(view, error.getErrorCode(), error.getDescription().toString(), request.getUrl().toString());
// view.loadUrl("file:///android_asset/no_internet.html");
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webViewError = true;
}
}