2017-04-26 145 views
-4

我编写了一个Java程序从tumblr获取JSON列表。它运行良好5-6个月,但现在是一个错误。我试图从正面删除22个无用的字符,并从最后删除1个分号。java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-23错误

String input = ("good.tumblr.com"); 

String urlContent = urlReader("http://"+input+"/api/read/json?type=photo&num=0&start=0"); 

urlContent=urlContent.substring(22,urlContent.length()-1); 

但我正在逐渐

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -23 
    at java.lang.String.substring(String.java:1967) 
    at main.main(main.java:27) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

这是字符串我会得到

" var tumblr_api_read = {"tumblelog":{"title":"","description":"Live Well. Do Good.","name":"good","timezone":"US\/Pacific","cname":false,"feeds":[]},"posts-start":0,"posts-total":2184,"posts-type":"photo","posts":[]}; " 

https://github.com/Shiva7071007/tumloader_java/tree/master/src

链接,整个程序。其在乞讨,但其无法正常工作现在工作。代码没有优化,因为这是我的第一个Java代码。

+5

你可以发布[mcve] – 2017-04-26 15:39:12

+2

关于错误消息有什么不清楚?什么是'urlReader'?在分配该函数的返回值之后,'urlContent'看起来像什么? – tnw

+0

您需要查看返回的字符串。我认为它的格式改变了。 –

回答

0

看起来像tumblr已移至https。因此,与其让机身采用JSON结构你只得到与重定向信息进行String urlContent持有空字符串""对你不串开始可以从22length()-1

要解决此问题,只需将您的请求从http更改为https

0

看起来像字符串返回是空白或更短,然后22个字符。在修改之前,您需要验证原始字符串

相关问题