要解决在Android应用程序中使用Google论坛短链接在WebView中无法正常工作的问题,可以尝试以下解决方法:
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("https://goo.gl/")) {
// 使用原始链接替换短链接
String expandedUrl = expandShortUrl(url);
if (expandedUrl != null) {
view.loadUrl(expandedUrl); // 加载原始链接
return true;
}
}
return super.shouldOverrideUrlLoading(view, url);
}
});
private String expandShortUrl(String shortUrl) {
try {
URL url = new URL(shortUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM) {
String expandedUrl = connection.getHeaderField("Location");
connection.disconnect();
return expandedUrl;
}
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这些解决方法可以确保Google论坛短链接在WebView中正常工作。请根据您的具体需求和应用程序的实现选择适合的解决方法。