最近我在编写一些抓取新浪微博的小程序,在获取某人微博时需要翻页,在Java SDK中有个参数是new Paging(1),每当我更改到N页的时候,都会报出数组越界错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at weibo4j.model.Source.<init>(Source.java:13)
at weibo4j.model.Status.constructJson(Status.java:87)
at weibo4j.model.Status.<init>(Status.java:132)
at weibo4j.model.Status.constructJson(Status.java:102)
at weibo4j.model.Status.<init>(Status.java:132)
at weibo4j.model.Status.constructStatuses(Status.java:240)
at weibo4j.Timeline.getUserTimeline(Timeline.java:206)
经过到网上搜索发现有网友已经解决了这个问题。此网友的说明如下:
先回答原因:
因為當中有些微博已被刪除,返回的json 是:
{"created_at":"","id":3373562566121134,"mid":"3373562566121134","idstr":"3373562566121134","text":"此微博已被删除。","deleted":"1"},"annotations":[],"reposts_count":1751,"comments_count":669,"mlevel":0}
weibo4j.model.Status.constructJson(Status.java:87)
這個是關於source 的,由於source 沒有返回,
所以在source.java 的constructor 中
String[] source = str.split("\"",5);
這裡出現ArrayIndexOutOfBoundsException
解決方法就是修改source.java
但由於我不熟悉java 的syntax , 可能有錯
String[] source = str.split("\"",5);
url = source[1];
relationShip = source[3];
name = source[4].replace(">", "").replace("</a", "");
改為
if(str.contains("\""))
{
String[] source = str.split("\"",5);
url = source[1];
relationShip = source[3];
name = source[4].replace(">", "").replace("</a", "");
}
else
{
url = "";
relationShip = "";
name = "";
}
如此便可解决了。