我编写了java类来计算给定网站的响应时间并将数据存储在文件中。所以我想调用正在计算的方法。它也有争论。当我在main方法中调用该方法时,它显示出一些错误。有人可以修复我的错误吗?这是我的代码。
public class NewClass1 {
private static int start = 0;
private static int finish = 0;
public static int[] time1=new int[20];
private static DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
public static String[] today = new String[4];
public static NewClass2 myclass(String website) throws Exception {
FileWriter fw = new FileWriter("google.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("current time response time");
NewClass2 getData=new NewClass2();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
for(int i=0;i<20;i++)
{
String stack_url = website;
try {
URL url = new URL(stack_url);
HttpURLConnection httpUrlConnect = (HttpURLConnection) url.openConnection();
httpUrlConnect.setConnectTimeout(5000);
start = (int) System.currentTimeMillis();
httpUrlConnect.connect();
if (httpUrlConnect.getResponseCode() == 200)
{
finish = (int) System.currentTimeMillis();
System.out.println(stack_url + " - " + httpUrlConnect.getResponseMessage() + " took " + (finish-start) + " Milli Seconds.");
}
if (httpUrlConnect.getResponseCode() == httpUrlConnect.HTTP_NOT_FOUND)
{
System.out.println(stack_url + " - " + httpUrlConnect.getResponseMessage() + " - " + httpUrlConnect.HTTP_NOT_FOUND);
}
}
catch (Exception e)
{
}
LocalDateTime now = LocalDateTime.now();
finish = (int) System.currentTimeMillis();
System.out.println(stack_url + " took "+ (finish-start)+" milliseconds. at "+dtf.format(now));
String day = dtf.format(now);
try{
bw.write(day+" ");
bw.write(finish-start);
}
catch (IOException e) {
System.err.println("Problem writing to the file statsTest.txt");
}
today[i]= dtf.format(now);
time1[i]=finish-start;
Thread.sleep(6000);
}
bw.close();
fw.close();
getData.settime(time1);
getData.settoday(today);
return getData;
}
public static void main(String[] args) throws IOException{
NewClass1 mynewclass =new NewClass1();
mynewclass.myclass();
}
}
答案 0 :(得分:1)
在myclass方法中,它需要一个String参数:
方法myclass
public static NewClass2 myclass(String website) throws Exception {
//your code
}
因此,当你调用它时,你应该传入String参数,如下所示:
主要方法
public static void main(String[] args) throws IOException{
NewClass1 mynewclass =new NewClass1();
String website = "https://www.google.com";
MyClass2 yourVariableName = mynewclass.myclass(website);
}
因为你也返回一个变量,所以你应该像这样保持它
答案 1 :(得分:0)
myclass
函数采用String
参数。在main
中,您可以使用无参数调用它。