Java的格式不正确的URL例外 [英] Java Malformed URL Exception
问题描述
我试图在一个Android应用程序我建立一个HTTP POST请求,但无论我使用的要求是什么网址,Eclipse的不断提高了错误的URL例外。我已经尝试了线code从Android的教程之一:
I'm trying to make an http POST request in an android app I'm building, but no matter what url I use for the request, Eclipse keeps raising a Malformed URL Exception. I've tried a line of code from one of the android tutorials:
URL url = new URL("https://wikipedia.org");
和甚至触发错误。还有一个原因是Eclipse的保留,因为我尝试创建的任何URL提出这个错误?
And even that triggers the error. Is there a reason Eclipse keeps raising this error for any URL I try to create?
推荐答案
这不是养错误,它的抱怨,你有没有处理的the可能性,这可能 ,即使不会,因为在这种情况下,该URL不是畸形。 Java的似乎认为这是一个好主意。 (事实并非如此。)
It is not raising the error, it's complaining that you haven't handled the possibility that it might, even though it won't, because the URL in this case is not malformed. Java seems to think this is a good idea. (It's not.)
要掩上了,添加抛出MalformedURLException的
或抛出IOException异常
的方法声明。例如:
To shut it up, add throws MalformedURLException
or throws IOException
to the method declaration. E.g.:
public void myMethod() throws IOException {
URL url = new URL("https://wikipedia.org/");
}
另外,捕获并重新抛出异常作为非检查的异常:
Alternatively, catch and rethrow the exception as a non-checked exception:
try {
URL url = new URL("https://wikipedia.org/");
...
} catch (IOException e) {
throw new RuntimeException(e);
}
这篇关于Java的格式不正确的URL例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!