gpt4 book ai didi

Java的URL无法正确解析字符串

转载 作者:行者123 更新时间:2023-12-02 04:10:43 25 4
gpt4 key购买 nike

我正在创建一个 URL 变量:

URL inputURL = null;
try {
inputURL = new URL(inputUrlString);
} catch (MalformedURLException e) {
Log.e(TAG, "Bad Parsing.");
e.printStackTrace();

AlertDialog ad = new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("URL is not HTTP-like url.")
.setCancelable(true).create();
ad.show();
}

如果 inputUrlString"http:""http:/""http:/rubbish" > 它会解析它,就像它没问题一样,更进一步并粉碎一切。它真的是一个有效的 URL 吗?通过 Pattern 类解析它是一个好的做法吗?

最佳答案

Throws:
MalformedURLException - if no protocol is specified, or an unknown protocol is found, or spec is null.

正如您在 the URL javadoc 中看到的那样构造函数本身相当宽松。

您可以使用 apache common 的 UrlValidator ,或者只是在使用 URL 时注意错误。

关于Java的URL无法正确解析字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com