这两天抓取新浪微博粉丝,结果碰到两个棘手的问题,三四天终于给解决了
首先就是
public String asString() throws WeiboException{
if(null == responseAsString){
BufferedReader br;
InputStream stream = null;
try {
stream = asStream();
if (null == stream) {
return null;
}
br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
StringBuffer buf = new StringBuffer();
String line;
while (null != (line = br.readLine())) {
buf.append(line).append("\n");
}
this.responseAsString = buf.toString();
if(Configuration.isDalvik()){
this.responseAsString = unescape(responseAsString);
}
// // log(responseAsString);
stream.close();
con.disconnect();
streamConsumed = true;
} catch (NullPointerException npe) {
// don't remember in which case npe can be thrown
throw new WeiboException(npe.getMessage(), npe);
} catch (IOException ioe) {
throw new WeiboException(ioe.getMessage(), ioe);
}
}
return responseAsString;
}
这个是通过post请求获取res后转换为string的,中间注掉的那行出的问题,跳进去发现,居然是systemout输出,每次查询回来的数据,就这么打印出了,结果导致eclipse在第三次或第四次查询后就会直接挂掉,不停的new string,不挂掉才怪,这个问题超级难找啊,不得不说这api写得真是让人无语
另外一个问题更让人郁闷,在本地测好了,ok扔linux服务器上,哎,问题来了
出现空指针,然后顺着空指针查问题,居然发现有个exception还没有打印,只有捕获不打印啊,⊙﹏⊙b汗
问题出现了
java.security.NoSuchAlgorithmException: Algorithm HmacSHA1 not available
at javax.crypto.Mac.getInstance(DashoA13*..)
这个纠结时间最长了,百度谷歌另加发帖求助,无所不用其极啊,大都说是jdk问题,不过他们的都是本地环境
那好,咱反复下了几个jdk结果还真不管用
无奈,试来试去,另加主查linux出现的问题,终于找到一帖子,classpath下加上jce.jar跟sunjce_provider.jar可解决
忙着配置classpath,后来一想,反正脚本用的
java -cp . -Djava.ext.dirs=lib ···· 直接把这俩jar包扔进lib目录下,运行,终于ok了