我在这里做错了什么?请记住,我通常使用C#而不是Java进行编码。我现在需要将我的C#转换为Java。
Java代码:
public class HelloWorld{
public static void main(String []args)
{
System.out.println("Hello World");
boolean output;
try
{
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
output = true;
}
catch(Exception e)
{
output = false;
}
System.out.println("Result: " + Boolean.toString(output));
}
}
C#代码为:
using System.Net;
bool output;
try
{
WebClient wc = new System.Net.WebClient();
string webData = wc.DownloadString("URL");
output = true;
}
catch
{
output = false;
}
Console.WriteLine("Result: " + output.ToString());
[
$javac HelloWorld.java
HelloWorld.java:9: error: cannot find symbol
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
^
symbol: class URL
location: class HelloWorld
HelloWorld.java:9: error: cannot find symbol
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
^
symbol: class URL
location: class HelloWorld
HelloWorld.java:10: error: cannot find symbol
URLConnection con = url.openConnection();
^
symbol: class URLConnection
location: class HelloWorld
HelloWorld.java:11: error: cannot find symbol
InputStream is =con.getInputStream();
^
symbol: class InputStream
location: class HelloWorld
4 errors
在此站点上编译时https://www.tutorialspoint.com/compile_java_online.php
Using System.Net;
,您是[[import
类。在这里,您尚未导入URL
或URL
或URLConnection
。基本上,添加到您的JavaURLConnection