2017-04-26 142 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個字符。在修改之前,您需要驗證原始字符串

相關問題