2012-12-11 55 views
0

我試圖根據SearchURL變量中指定的搜索條件獲取隨機推文。但是,當我運行該方法時,它不會返回結果。我知道錯誤發生在這條線上:空指針從twitter獲取搜索結果的例外

HttpResponse tweetResponse = tweetClient.execute(tweetGet); 

但我不知道什麼事情出錯了。在獲取信息和解碼信息時,一切都應該正常工作。任何人有任何想法?考慮這是一個挑戰。

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

或者,如果任何人都可以提出一種替代方法,我可以用它來獲取基於指定的搜索或指定的用戶,這將是一樣有益的隨機鳴叫!

+1

請發佈您的LogCat錯誤。 – Sam

回答

1

你叫

HttpGet tweetGet = new HttpGet(); 

,然後你不把任何東西tweetGet。你不會調用setURI或任何東西。 tweetClient希望HttpGet有一個url,但它是空的。因此空指針異常。

1

讓我們看看你的要求:

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

這正是要求什麼,沒有指定的URL,虛無縹緲。你需要指定一個URL,否則這是行不通的。