我创建了一个关于从网页获取元素的java应用程序,这就是代码。
public class rpms {
public rpms() {
}
private static final String USER_AGENT = "Mozilla/5.0";
private static final String URL ="https://url.com";
public static void main(String[] args) {
URLget labaccess = new URLget();
try {
getElementTd(sendGetRequest(URL).toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String sendGetRequest(String urlString) throws IOException {
URL obj = new URL(urlString);
HttpURLConnection httpConnection = (HttpURLConnection) obj
.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = httpConnection.getResponseCode();
if (responseCode == 200) {
BufferedReader responseReader = new BufferedReader(
new InputStreamReader(httpConnection.getInputStream()));
String responseLine;
StringBuffer response = new StringBuffer();
while ((responseLine = responseReader.readLine()) != null) {
response.append(responseLine + "n");
}
responseReader.close();
// print result
return response.toString();
}
return null;
}
public static void getElementTd(String sourceCode)
throws FileNotFoundException, UnsupportedEncodingException {
String fragment = sourceCode;
ArrayList<String> ip = new ArrayList<String>();
Document doc = Jsoup.parseBodyFragment(fragment);
Elements elements = doc.select("td p");
for (int i = 0; i < elements.size(); i++) {
// System.out.println(i+1+" ) "+elements.eq(i).text().toString());
if (fileExplode(elements.eq(i).text().toString())) {
System.out.println(elements.eq(i).text().toString());
}
}
}
public static boolean fileExplode(String str1) {
boolean hasRPM = false;
String[] split1 = str1.replace(".", " ").split(" ");
for (int i = 0; i < split1.length; i++) {
if ((i + 1) == split1.length) {
if (split1[i].equalsIgnoreCase("rpm")
|| (split1[i].charAt(0) == 'r'
&& split1[i].charAt(1) == 'p' && split1[i]
.charAt(2) == 'm')) {
hasRPM = true;
}break;
}
}
return hasRPM;
}
}
但是当我执行上面的代码时。出现错误:线程"main"中出现异常我不知道这是什么意思。charAt出错。
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at rpms.fileExplode(rpms.java:98)
at rpms.getElementTd(rpms.java:82)
at rpms.main(rpms.java:32)
为了得到更多的指导,这是发生错误的数字。
/*97*/ if (split1[split1.length - 1].equalsIgnoreCase("rpm")
/*98*/|| (split1[i].charAt(0) == 'r'
/*99*/ && split1[i].charAt(1) == 'p' && split1[i]
/*100*/ .charAt(2) == 'm')) {
/*82*/ if (fileExplode(elements.eq(i).text().toString())) {
/*83*/ System.out.println(elements.eq(i).text().toString());
/*84*/ }
/*32*/ getElementTd(sendGetRequest(URL).toString());
请帮我解决发生的错误。
此错误表示您试图从索引大于或等于字符串长度的字符串中获取字符。具体来说,就是当您试图在第98行获取split[i]
中的第一个字符时。您永远不会检查split1[i]
的大小是否大于0。我建议补充一下。
for (int i = 0; i < split1.length; i++) {
if ((i + 1) == split1.length) {
if (split1[i].equalsIgnoreCase("rpm")
|| (split1[i].length > 2 && split1[i].charAt(0) == 'r'
&& split1[i].charAt(1) == 'p' && split1[i]
.charAt(2) == 'm')) {
hasRPM = true;
}
break;
}
}