Search code examples
androidtwittertwitter4j

Twitter4j , searching for tweets with keyword


I am making a twitter project, to search all the public tweets with a specific keyword. The following crashes my app:

 private void searchTweets() {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey(getString(R.string.twitter_consumer_key))
                .setOAuthConsumerSecret(getString(R.string.twitter_consumer_secret))
                .setOAuthAccessToken(getString(R.string.twitter_access_token))
                .setOAuthAccessTokenSecret(getString(R.string.twitter_token_secret));

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();


        try {
            Query query = new Query("India");
            QueryResult result;
            do {
                result = twitter.search(query);
                List<Status> tweets = result.getTweets();
                for (Status tweet : tweets) {
                   // System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                    Log.e("TweetSearch",tweet.getUser().getScreenName() + " - " + tweet.getText() ) ;

                }
            } while ((query = result.nextQuery()) != null);
            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
           // System.out.println("Failed to search tweets: " + te.getMessage());

            Log.e("TweetSearch", te.getMessage());
            System.exit(-1);
        }
    }

I want to display the tweets in a list. I have taken care of all the authentications required, and the app authentication flow is alright. How can I know the JSON data of the searched tweets so that I can parse it into my list?

I tried this, How to implement the asyntask?

public class LimetrayTweets extends Activity {

    public static final String TAG = "TweetSearch";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_limetray_tweets);

        SearchTweet("Limetray");



    }

    public static String SearchTweet(String searchTerm) {
        HttpURLConnection httpConnection = null;
        BufferedReader bufferedReader = null;
        StringBuilder response = new StringBuilder();

        try {



            URL url = new URL(Constants.URL_SEARCH +  URLEncoder.encode("#" + searchTerm) + "&result_type=mixed&lang=en");
            Log.e(TAG, "url twitter search: " + url.toString());

            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("GET");


            String jsonString = appAuthentication();
            JSONObject jsonObjectDocument = new JSONObject(jsonString);
            String token = jsonObjectDocument.getString("token_type") + " " +
                    jsonObjectDocument.getString("access_token");

            httpConnection.setRequestProperty("Authorization", token);
            httpConnection.setRequestProperty("Content-Type", "application/json");
            httpConnection.connect();

            bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));

            String line;
            while ((line = bufferedReader.readLine()) != null){
                response.append(line);
            }

            Log.d(TAG, "GET response code: " + String.valueOf(httpConnection.getResponseCode()));
            Log.d(TAG, "JSON response: " + response.toString());


        } catch (Exception e) {
            Log.e(TAG, "GET error: " + Log.getStackTraceString(e));

        }finally {
            if(httpConnection != null){
                httpConnection.disconnect();

            }
        }

        return response.toString();
    }

    public static String appAuthentication(){

        HttpURLConnection httpConnection = null;
        OutputStream outputStream = null;
        BufferedReader bufferedReader = null;
        StringBuilder response = null;

        try {
            URL url = new URL(Constants.URL_AUTHENTICATION);
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);

            String accessCredential = Constants.CONSUMER_KEY + ":" + Constants.CONSUMER_SECRET;
            String authorization = "Basic " + Base64.encodeToString(accessCredential.getBytes(), Base64.NO_WRAP);
            String param = "grant_type=client_credentials";

            httpConnection.addRequestProperty("Authorization", authorization);
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            httpConnection.connect();

            outputStream = httpConnection.getOutputStream();
            outputStream.write(param.getBytes());
            outputStream.flush();
            outputStream.close();
//            int statusCode = httpConnection.getResponseCode();
//            String reason =httpConnection.getResponseMessage();

            bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
            String line;
            response = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null){
                response.append(line);
            }

            Log.d(TAG, "POST response code: " + String.valueOf(httpConnection.getResponseCode()));
            Log.d(TAG, "JSON response: " + response.toString());

        } catch (Exception e) {
            Log.e(TAG, "POST error: " + Log.getStackTraceString(e));

        }finally{
            if (httpConnection != null) {
                httpConnection.disconnect();
            }
        }
        return response.toString();
    }

Solution

  • Twitter4j successful implementation:

    @Override
    protected ArrayList<String> doInBackground(String... arg0) {
    
        List<twitter4j.Status> tweets = new ArrayList();
        tweetTexts.clear();
    
        Twitter mTwitter = getTwitter();
        try {
    
            tweets = mTwitter.search(new Query(searchText)).getTweets();
            for (twitter4j.Status t : tweets) {
            tweetTexts.add(t.getText() + "\n\n");
            }
    
        } catch (Exception e) {
            Log.e("Error", "Exception");
        }
    
    
    
        return tweetTexts;
    }