-4

I wrote a Java program to get JSON list from tumblr. It worked well for 5-6 month but now is giving an error. I am trying to remove 22 useless character from front and 1 semicolon from end.

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);

But I am getting

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)

This is the string i will get

"  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

link to whole program .its work in the begging but its not working now. code is not optimized as this was my first java code.

8
  • 5
    Can you post a minimal reproducible example Commented Apr 26, 2017 at 15:39
  • 2
    What's unclear about the error message? What is urlReader? What does urlContent look like after you assign the return value from that function? Commented Apr 26, 2017 at 15:39
  • you need to look at the string that was returned. I assume its format changed. Commented Apr 26, 2017 at 15:40
  • 2
    The problem is, you've got a string index that's out of range. Commented Apr 26, 2017 at 15:40
  • View your urlReader method. I suppose the error is coming from there. Your call to substring is using a string different fro what you wanted. Commented Apr 26, 2017 at 15:52

2 Answers 2

0

It looks like tumblr moved to https. So instead of getting body with JSON structure you are getting only headers with redirection information making String urlContent hold empty string "" on which you can't substring starting from 22 till length()-1.

To solve this issue simply change your request from http to https.

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like the string return is blank or shorter then 22 characters. You will need to validate your original string before modifying it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.