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");
And even that triggers the error. Is there a reason Eclipse keeps raising this error for any URL I try to create?
解决方案
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.)
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);
}