我正在尝试使用以下代码从 URL 下载 Web 内容:
URL content = new URL(urlstr);
HttpURLConnection urlConnection = (HttpURLConnection) content.openConnection();
try {
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
String existing_string = "";
StringBuilder builder = new StringBuilder(existing_string);
int data = isw.read();
while (data != -1) {
char current = (char) data;
builder.append(current);
data = isw.read();
}
我收到的错误是 Cannot resolve method openConnection()
。导入语句如下所示:
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
我查看了几乎所有与此相关的问题,但我似乎没有让它发挥作用。
最佳答案
您必须有一个属于您自己的名为 URL
的类,可能就是这个。不要那样做。不要将现有的 JDK 类名用作您自己的类名。
关于java - 无法解析方法 openConnection(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40541283/