2014-09-06 2 views
0

그래서 사용자의 타임 라인을 검색하는 데 트위터 API를 사용하고 있습니다. 사용자가 실제 트윗과 함께 보내는 모든 응답을 검색하는 것처럼 보입니다. 응답을 어떻게 제거합니까?내 트위터 피드에서 답장을 검색/표시하지 않으려면 어떻게합니까?

MainTwitterActivity.java

public class MainTwitterActivity extends ListFragment { 

    private ListFragment fragment; 
    final static String ScreenName = "the screen name"; 
    final static String LOG_TAG = "aka"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     fragment = this; 

     downloadTweets(); 
    } 

    // download twitter timeline after first checking to see if there is a network connection 
    public void downloadTweets() { 
     ConnectivityManager connMgr = (ConnectivityManager)getActivity() 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 

     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

     if (networkInfo != null && networkInfo.isConnected()) { 
      new DownloadTwitterTask().execute(ScreenName); 
     } else { 
      Log.v(LOG_TAG, "No network connection available."); 
     } 
    } 

    // Uses an AsyncTask to download a Twitter user's timeline 
    private class DownloadTwitterTask extends AsyncTask<String, Void, String> { 
     final static String CONSUMER_KEY = "My Key"; 
     final static String CONSUMER_SECRET = "My Secret"; 
     final static String TwitterTokenURL = "https://api.twitter.com/oauth2/token"; 
     final static String TwitterStreamURL = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name="; 

     @Override 
     protected String doInBackground(String... screenNames) { 
      String result = null; 

      if (screenNames.length > 0) { 
       result = getTwitterStream(screenNames[0]); 
      } 
      return result; 
     } 

     // onPostExecute convert the JSON results into a Twitter object (which is an Array list of tweets 
     @Override 
     protected void onPostExecute(String result) { 
      Twitter twits = jsonToTwitter(result); 

      // lets write the results to the console as well 
      for (Tweet tweet : twits) { 
       Log.i(LOG_TAG, tweet.getText()); 
      } 

      // send the tweets to the adapter for rendering 
      ArrayAdapter<Tweet> adapter = new ArrayAdapter<Tweet>(getActivity(), android.R.layout.simple_list_item_1, twits); 
      setListAdapter(adapter); 
     } 

     // converts a string of JSON data into a Twitter object 
     private Twitter jsonToTwitter(String result) { 
      Twitter twits = null; 
      if (result != null && result.length() > 0) { 
       try { 
        Gson gson = new Gson(); 
        twits = gson.fromJson(result, Twitter.class); 
       } catch (IllegalStateException ex) { 
        // just eat the exception 
       } 
      } 
      return twits; 
     } 

     // convert a JSON authentication object into an Authenticated object 
     private Authenticated jsonToAuthenticated(String rawAuthorization) { 
      Authenticated auth = null; 
      if (rawAuthorization != null && rawAuthorization.length() > 0) { 
       try { 
        Gson gson = new Gson(); 
        auth = gson.fromJson(rawAuthorization, Authenticated.class); 
       } catch (IllegalStateException ex) { 
        // just eat the exception 
       } 
      } 
      return auth; 
     } 

     private String getResponseBody(HttpRequestBase request) { 
      StringBuilder sb = new StringBuilder(); 
      try { 

       DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams()); 
       HttpResponse response = httpClient.execute(request); 
       int statusCode = response.getStatusLine().getStatusCode(); 
       String reason = response.getStatusLine().getReasonPhrase(); 

       if (statusCode == 200) { 

        HttpEntity entity = response.getEntity(); 
        InputStream inputStream = entity.getContent(); 

        BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
        String line = null; 
        while ((line = bReader.readLine()) != null) { 
         sb.append(line); 
        } 
       } else { 
        sb.append(reason); 
       } 
      } catch (UnsupportedEncodingException ex) { 
      } catch (ClientProtocolException ex1) { 
      } catch (IOException ex2) { 
      } 
      return sb.toString(); 
     } 

     private String getTwitterStream(String screenName) { 
      String results = null; 

      // Step 1: Encode consumer key and secret 
      try { 
       // URL encode the consumer key and secret 
       String urlApiKey = URLEncoder.encode(CONSUMER_KEY, "UTF-8"); 
       String urlApiSecret = URLEncoder.encode(CONSUMER_SECRET, "UTF-8"); 

       // Concatenate the encoded consumer key, a colon character, and the 
       // encoded consumer secret 
       String combined = urlApiKey + ":" + urlApiSecret; 

       // Base64 encode the string 
       String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP); 

       // Step 2: Obtain a bearer token 
       HttpPost httpPost = new HttpPost(TwitterTokenURL); 
       httpPost.setHeader("Authorization", "Basic " + base64Encoded); 
       httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 
       httpPost.setEntity(new StringEntity("grant_type=client_credentials")); 
       String rawAuthorization = getResponseBody(httpPost); 
       Authenticated auth = jsonToAuthenticated(rawAuthorization); 

       // Applications should verify that the value associated with the 
       // token_type key of the returned object is bearer 
       if (auth != null && auth.token_type.equals("bearer")) { 

        // Step 3: Authenticate API requests with bearer token 
        HttpGet httpGet = new HttpGet(TwitterStreamURL + screenName); 

        // construct a normal HTTPS request and include an Authorization 
        // header with the value of Bearer <> 
        httpGet.setHeader("Authorization", "Bearer " + auth.access_token); 
        httpGet.setHeader("Content-Type", "application/json"); 
        // update the results with the body of the response 
        results = getResponseBody(httpGet); 
       } 
      } catch (UnsupportedEncodingException ex) { 
      } catch (IllegalStateException ex1) { 
      } 
      return results; 
     } 
    } 
} 

답변

1

당신은 당신이 매개 변수 exclude_replies=true

헤이 프레스토를 사용할 수있는 것을 볼 수 read the documentation 경우! 타임 라인에 더 이상 응답이 없습니다.

관련 문제