如何解决某些网址未加载到Webview中,但已加载到浏览器中
我正在尝试使用Webview加载报纸URL。这是我的网络视图设置:
public void submitOrder(View view) {
int number = 2;
display(number);
Price(2* 5);
}
/**
* This method displays the given quantity value on the screen.
*/
@SuppressLint("SetTextI18n")
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/**
* This method displays the given price on the screen.
*/
private void Price(int number) {
TextView priceEditText = (TextView) findViewById(R.id.price_text_view);
priceEditText.setText(NumberFormat.getCurrencyInstance().format(number));
}
我要加载的网址是:http://feeds.inquisitr.com/~r/google/yDYq/~3/1glI3navSEE/
但未加载,但是当我将此URL粘贴到浏览器中时,该URL被浏览器更改,并且完美加载。我还需要在Web视图中做些什么,以使其像浏览器一样,并更改(解码)URL。
ps:当我在浏览器中加载此URL时,URL变为“ https://www.inquisitr.com/6337911/big-brother-22-week-11-veto-winner/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed %3A + google%2FyDYq +%28The + Inquisitr +-+ News%29“
对此有什么解决方案?
解决方法
在上面的示例中,您使用的是缩短的URL。您需要获取最终URL才能加载到webView
中。您可以通过实现以下方法来获取最终URL:
public static String getFinalURL(String url) throws IOException {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setInstanceFollowRedirects(false);
con.connect();
con.getInputStream();
if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
String redirectUrl = con.getHeaderField("Location");
return getFinalURL(redirectUrl);
}
return url;
}
注意:如果存在多个重定向,此方法是递归的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。