In this section, you will learn how to get user's location via search API and user class.
Getting user location via search API and User class
In this section, you will learn how to get user's location via search API and user class.
In the below example, first we will search tweets about a particular hash tag using Query class. After getting tweets and the name of the users which did the tweet , we can get the location of the user using the user name fetched from the Query class. In the below code, you will see how you can do this.
CODE
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.sql.*,twitter4j.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% Twitter twitter = new TwitterFactory().getInstance(); Query query = new Query("#snbTo").rpp(25); QueryResult result = twitter.search(query); User user; int i = 1; for (Tweet tweet : result.getTweets()) { user = twitter.showUser(tweet.getFromUser()); out.println("[" + i + "]<img src='" + tweet.getProfileImageUrl() + "'>" + "<b>" + tweet.getFromUser() + "(" + tweet.getCreatedAt() + ")" + user.getLocation() + "</b>" + " : " + tweet.getText() + "<br/>"); i++; } %> </body> </html>
OUTPUT
[ 0 ] Comments