我是android新手。我已经建了一个项目。
我有一个班级,检查网址是否存在,如下:
public class connect {
Boolean checkServer() throws IOException
{
Boolean check = false;
HttpURLConnection connection = null;
try {
URL url = new URL("www.google.com");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
connection.getInputStream();
// do something with the input stream here
check = true;
} catch (MalformedURLException e1) {
e1.printStackTrace();
check = false;
}
finally {
if(null != connection) { connection.disconnect(); }
}
return check;
}
}
我想在eclipse中将其作为单个文件运行。
我该怎么做?
非常感谢
答案 0 :(得分:1)
您可以编写一个Junit类或以简单的方式编写一个带有public static void main()方法的新类,或者在同一个类和main()方法中创建一个类的对象并启动它main()方法
public class ConnectDemo {
/**
* @param args
*/
public static void main(String[] args) {
Connect c = new Connect();
if(c.checkServer())
System.out.println("Check Server Successful");
else
System.out.println("Check Server not Successful");
}
}