我创建了一个关于从网页获取元素的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());
请帮我解决发生的错误。
答案 0 :(得分:1)
错误意味着您尝试从索引处获取大于或等于字符串长度的字符串中的字符。具体来说,就是当你试图在第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;
}
}