根据Android参考,您可以使用字符串声明URL。
这意味着下一个代码应该是正确的(即):
URL url = new URL("http://www.android.com/");
当我尝试使用它时,我得到:
“ java.net.MalformedURLException”
它发生在AndroidStudio 1.2.1.1上
我要使用的完整代码来自Android的HttpURLConnect的引用:
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
看这个:
答案是:这不是引发错误,它是在抱怨您没有处理可能发生的错误(即使不会发生),因为这种情况下的URL格式不正确。Java似乎认为这是一个好主意。(它不是。)
要关闭它,请向方法声明中添加引发MalformedURLException或引发IOException。例如:
public void myMethod() throws IOException {
URL url = new URL("https://wikipedia.org/");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句