0

I'm trying to obtain a random tweet given the search terms specified in the SearchURL variable. However, when I run the method it doesn't return the results. I know that the error occurs on this line:

HttpResponse tweetResponse = tweetClient.execute(tweetGet);

but I have no idea what is going wrong. Everything should be working correctly in getting the information as well as decoding it. Anybody have any ideas? Consider it a challenge.

TextView caption;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pitter_home);

    // Create PitterImageFragment
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.main_image, new PitterImageFragment());
    transaction.commit();

    // Create Twitter Caption Reference
    caption = (TextView) findViewById(R.id.tweet_caption);

    // Create Creation Button
    Button button = (Button) findViewById(R.id.creation_button);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            FragmentManager manager = getFragmentManager();
            Fragment fragment = manager.findFragmentById(R.id.main_image);
            ((PitterImageFragment) fragment).generateImage();
            generateTweet();
        }// end onClick method
    });

}// end OnCreateMethod

public void generateTweet () {

    // Code Taken From: http://mobile.tutsplus.com/tutorials/android/build-a-twitter-search-app-project-setup/
    try {
        // Calculate what to Search for
        String searchURL = "http://search.twitter.com/search.json?q=jedi";
        new GetTweets().execute(searchURL);

    } catch(Exception e) {
        Log.d("CATCH", "Begin Code Error: something went wrong!");
        e.printStackTrace();
    }// end catch
}// end generateTweet method

protected void setCaption(String tweet) {
    caption.setText(tweet);
}// end setCaption method

private class GetTweets extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... twitterURL) {

        // Get Tweets from the Internet
        StringBuilder tweetFeedBuilder = new StringBuilder();

        try {
            Log.d("LINE WATCH", "entering try statement");
            HttpClient tweetClient = new DefaultHttpClient();
            Log.d("LINE WATCH", "line 1 completed");
            HttpGet tweetGet = new HttpGet();
            Log.d("LINE WATCH", "line 2 completed");
            Log.d("LINE WATCH", tweetGet.toString());
            HttpResponse tweetResponse = tweetClient.execute(tweetGet);
            Log.d("LINE WATCH", "line 3 completed");
            StatusLine searchStatus = tweetResponse.getStatusLine();
            Log.d("LINE WATCH", "line 4 completed");

            if (searchStatus.getStatusCode() == 200) {
                HttpEntity tweetEntity = tweetResponse.getEntity();
                InputStream tweetContent = tweetEntity.getContent();
                InputStreamReader tweetInput = new InputStreamReader(tweetContent);
                BufferedReader tweetReader = new BufferedReader(tweetInput);
                String lineIn;
                while ((lineIn = tweetReader.readLine()) != null) {
                    tweetFeedBuilder.append(lineIn);
                }// end while
            } else {
                Log.d("CATCH", "Get Status Code Error: something went wrong!");
            }// end if

        } catch(Exception e) {
            Log.d("CATCH", "Get Online Data Error: something went wrong!");
            e.printStackTrace();
        }// end catch
        return tweetFeedBuilder.toString();
    }// end doInBackground method

    protected void onPostExecute(String result) {
        // Decode the Results from the Search
        ArrayList<String> tweetList = new ArrayList<String>();
        try {
            JSONObject resultObject = new JSONObject(result.toString());
            JSONArray tweetArray = resultObject.getJSONArray("results");
            // Walk Through the Returned Tweets
            for (int t=0; t<tweetArray.length(); t++) {
                JSONObject tweetObject = tweetArray.getJSONObject(t);
                tweetList.add(tweetObject.get("text")+"\n\n");
            }// end for
        } catch (Exception e) {
            Log.d("CATCH", "Decode Results Error: something went wrong!");
            e.printStackTrace();
        }// end catch

        //TODO: TweetList size is always zero!?! WHY?
        // Select a Random Tweet from the Returned Ones
        Log.d("TWEETLIST", "TweetList size = " + tweetList.size());
        if (tweetList.size() > 0 ) {
            Random random = new Random();
            int number = random.nextInt(tweetList.size());
            setCaption(tweetList.get(number));
        } else {
            setCaption("No Tweets Found!");
        }// end if

    }// end onPostExecute method

}// end GetTweets class

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_pitter_home, menu);
    return true;
}

Or if anybody can suggest an alternative method I could use to obtain a random tweet based on a specified search or a specified user that would be just as helpful!

2 Answers2

1

You call

HttpGet tweetGet = new HttpGet();

and then you don't put anything in tweetGet. You don't call setURI or anything. tweetClient expects HttpGet to have a url, but it's null. Hence the null pointer exception.

Diego Basch
  • 12,764
  • 2
  • 29
  • 24
1

Let's look at your request:

HttpClient tweetClient = new DefaultHttpClient();
HttpGet tweetGet = new HttpGet();
HttpResponse tweetResponse = tweetClient.execute(tweetGet);

This requests exactly nothing, no URL specified, nada. You need to specify a URL, or else this just won't work.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142