5
I have an algorithm that does a search of old tweets on Twitter. The application worked normally for a few days, but out of nowhere it started to throw an exception.
Code:
public static List<Tweet> getTweets(String username, String since, String until, String querySearch) {
List<Tweet> results = new ArrayList<Tweet>();
try {
String refreshCursor = null;
while (true) {
JSONObject json = new JSONObject(getURLResponse(username, since, until, querySearch, refreshCursor));
refreshCursor = json.getString("scroll_cursor"); // <<--------
System.out.println("while");
Document doc = Jsoup.parse((String) json.get("items_html"));
Elements tweets = doc.select("div.js-stream-tweet");
if (tweets.size() == 0) {
break;
}
for (Element tweet : tweets) {
String usernameTweet = tweet.select("span.username.js-action-profile-name b").text();
String txt = tweet.select("p.js-tweet-text").text().replaceAll("[^\\u0000-\\uFFFF]", "");
int retweets = Integer.valueOf(tweet.select("span.ProfileTweet-action--retweet span.ProfileTweet-actionCount").attr("data-tweet-stat-count").replaceAll(",", ""));
int favorites = Integer.valueOf(tweet.select("span.ProfileTweet-action--favorite span.ProfileTweet-actionCount").attr("data-tweet-stat-count").replaceAll(",", ""));
long dateMs = Long.valueOf(tweet.select("small.time span.js-short-timestamp").attr("data-time-ms"));
Date date = new Date(dateMs);
Tweet t = new Tweet(usernameTweet, txt, date, retweets, favorites);
results.add(t);
}
}
} catch (Exception e) {
System.out.println("Error!");
}
return results;
}
On the line "<<--------
" the exception is being cast. The json object has the contents of the page returned, so I don’t know what’s going on.
This is the method that requests the page:
private static String getURLResponse(String from, String since, String until, String querySearch, String scrollCursor) throws Exception {
String appendQuery = "";
if (from != null) {
appendQuery += "from:"+from;
}
if (since != null) {
appendQuery += " since:"+since;
}
if (until != null) {
appendQuery += " until:"+until;
}
if (querySearch != null) {
appendQuery += " "+querySearch;
}
String url = String.format("https://twitter.com/i/search/timeline?f=realtime&q=%s&src=typd&scroll_cursor=%s", URLEncoder.encode(appendQuery, "UTF-8"), scrollCursor);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
Exception Try/catch:
twitter4j.JSONException: JSONObject["scroll_cursor"] not found.
at twitter4j.JSONObject.get(JSONObject.java:390)
at twitter4j.JSONObject.getString(JSONObject.java:504)
at Manager.TweetManager.getTweets(TweetManager.java:83)
at Main.Main.main(Main.java:52)
Put a
e.printStackTrace();
within yourcatch
and post here what appears. If possible, also put theString
which is returned bygetURLResponse(...)
.– Victor Stafusa
Basically, there’s nothing called "scroll_cursor" in the JSON you downloaded. I think it would be important for you to put in question an example of JSON downloaded with the
getURLResponse(...)
.– Victor Stafusa
I am unable to print the string of the json object. I can only see the content in Debug mode. json.getString() requires a string per parameter. You know which way to go?
– Roger Rubens
Try something like this:
String sj = getURLResponse(username, since, until, querySearch, refreshCursor);
System.out.println(sj);
JSONObject json = new JSONObject(sj);
and then I put in question what comes out in theprintln
.– Victor Stafusa
It worked. It’s a giant string, I think there’s no way I can post it here. It’s like attaching a . txt here at stackoverflow?
– Roger Rubens
You can post it on Pastebin.com and link here.
– Victor Stafusa
I put a Dropbox link on it anyway.
– Roger Rubens
Let’s go continue this discussion in chat.
– Roger Rubens