@Override
    protected ResponseList<DirectMessage> doInBackground(Void... params) {
      try {
        Twitter twitter = TwitterManager.getTwitter();

        // 受信したDM
        Paging directMessagesPaging = new Paging();
        if (mDirectMessagesMaxId > 0 && !mReloading) {
          directMessagesPaging.setMaxId(mDirectMessagesMaxId - 1);
          directMessagesPaging.setCount(BasicSettings.getPageCount() / 2);
        } else {
          directMessagesPaging.setCount(10);
        }
        ResponseList<DirectMessage> directMessages =
            twitter.getDirectMessages(directMessagesPaging);
        for (DirectMessage directMessage : directMessages) {
          if (mDirectMessagesMaxId <= 0L || mDirectMessagesMaxId > directMessage.getId()) {
            mDirectMessagesMaxId = directMessage.getId();
          }
        }

        // 送信したDM
        Paging sentDirectMessagesPaging = new Paging();
        if (mSentDirectMessagesMaxId > 0 && !mReloading) {
          sentDirectMessagesPaging.setMaxId(mSentDirectMessagesMaxId - 1);
          sentDirectMessagesPaging.setCount(BasicSettings.getPageCount() / 2);
        } else {
          sentDirectMessagesPaging.setCount(10);
        }
        ResponseList<DirectMessage> sentDirectMessages =
            twitter.getSentDirectMessages(sentDirectMessagesPaging);
        for (DirectMessage directMessage : sentDirectMessages) {
          if (mSentDirectMessagesMaxId <= 0L || mSentDirectMessagesMaxId > directMessage.getId()) {
            mSentDirectMessagesMaxId = directMessage.getId();
          }
        }

        directMessages.addAll(sentDirectMessages);

        // 日付でソート
        Collections.sort(
            directMessages,
            new Comparator<DirectMessage>() {

              @Override
              public int compare(DirectMessage arg0, DirectMessage arg1) {
                return arg1.getCreatedAt().compareTo(arg0.getCreatedAt());
              }
            });
        return directMessages;
      } catch (OutOfMemoryError e) {
        return null;
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }
 @Override
 public synchronized List<ParcelableStatus> loadInBackground() {
   final List<ParcelableStatus> data = getData();
   final long account_id = getAccountId();
   ResponseList<Status> statuses = null;
   try {
     final Paging paging = new Paging();
     final SharedPreferences prefs =
         getContext().getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
     final int load_item_limit =
         prefs.getInt(PREFERENCE_KEY_LOAD_ITEM_LIMIT, PREFERENCE_DEFAULT_LOAD_ITEM_LIMIT);
     paging.setCount(load_item_limit);
     if (mMaxId != -1) {
       paging.setMaxId(mMaxId);
     }
     statuses = getStatuses(paging);
   } catch (final TwitterException e) {
     e.printStackTrace();
   }
   if (statuses != null) {
     for (final Status status : statuses) {
       data.add(new ParcelableStatus(status, account_id, false));
     }
   }
   Collections.sort(data);
   return data;
 }
示例#3
0
  private ArrayAdapter<String> getTimeline() {
    SharedPreferences sp = getSharedPreferences("token", MODE_PRIVATE);
    String token = sp.getString("token", "");
    String tokenSecret = sp.getString("token_seacret", "");

    // twitterオブジェクトの作成
    Twitter tw = new TwitterFactory().getInstance();

    // AccessTokenオブジェクトの作成
    AccessToken at = new AccessToken(token, tokenSecret);

    // Consumer keyとConsumer key seacretの設定
    tw.setOAuthConsumer("iy2FEHXmSXNReJ6nYQ8FRg", "KYro4jM8BHlLSMsSdTylnTcm3pYaTCiG2UZrYK1yI4");

    // AccessTokenオブジェクトを設定
    tw.setOAuthAccessToken(at);

    try {
      // TLの取得
      ResponseList<Status> homeTl = tw.getHomeTimeline();

      ArrayAdapter<String> adapter =
          new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);

      for (Status status : homeTl) {
        // つぶやきのユーザーIDの取得
        String userName = status.getUser().getScreenName();
        // つぶやきの取得
        String tweet = status.getText();
        adapter.add("ユーザーID:" + userName + "\r\n" + "tweet:" + tweet);
      }
      Status s = homeTl.get(homeTl.size());
      Paging p = new Paging();
      p.setMaxId(s.getId());

      homeTl = null;
      homeTl = tw.getHomeTimeline(p);

      for (Status status : homeTl) {
        // つぶやきのユーザーIDの取得
        String userName = status.getUser().getScreenName();
        // つぶやきの取得
        String tweet = status.getText();
        adapter.add("ユーザーID:" + userName + "\r\n" + "tweet:" + tweet);
      }

      return adapter;
    } catch (TwitterException e) {
      e.printStackTrace();
      if (e.isCausedByNetworkIssue()) {
        Toast.makeText(getApplicationContext(), "ネットワークに接続して下さい", Toast.LENGTH_LONG);
      } else {
        Toast.makeText(getApplicationContext(), "エラーが発生しました。", Toast.LENGTH_LONG);
      }
    }
    return null;
  }
    @Override
    protected List<HaikuStatus> doInBackground(Void... params) {

      twitter4j.Status lastStatus = mAdapter.getItem(mAdapter.getCount() - 1).getStatus();

      Paging paging = new Paging();
      paging.setMaxId(lastStatus.getId() - 1);
      ResponseList<twitter4j.Status> timeline;

      try {
        timeline = mTwitter.getUserTimeline(userScreenName, paging);
      } catch (TwitterException e) {
        Log.e(TAG, e.toString());
        isThereTweetException = true;
        return null;
      }

      MorphologicalAnalysisByGooAPI analyzer =
          new MorphologicalAnalysisByGooAPI(getString(R.string.goo_id));
      List<HaikuStatus> haikuStatusList = new ArrayList<>();

      if (canCreateHaiku) {
        for (twitter4j.Status status : timeline) {
          try {
            List<Word> list =
                analyzer.analyze(
                    status.getRetweetedStatus() != null
                        ? status.getText().replaceFirst("RT", "")
                        : status.getText());
            String haiku = new HaikuGeneratorByGooAPI(list).generateHaikuStrictly();
            haikuStatusList.add(new HaikuStatus(haiku, status));
          } catch (IOException e) {
            Log.d(TAG, e.toString());
          }
        }
      } else {
        for (twitter4j.Status status : timeline) {
          String haiku = "";
          haikuStatusList.add(new HaikuStatus(haiku, status));
        }
      }

      return haikuStatusList;
    }
 @Override
 public List<ParcelableStatus> loadInBackground() {
   final List<ParcelableStatus> data = getData();
   final long account_id = getAccountId();
   ResponseList<Status> statuses = null;
   try {
     final Paging paging = new Paging();
     final SharedPreferences prefs =
         getContext().getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
     final int load_item_limit =
         prefs.getInt(PREFERENCE_KEY_LOAD_ITEM_LIMIT, PREFERENCE_DEFAULT_LOAD_ITEM_LIMIT);
     paging.setCount(load_item_limit);
     if (mMaxId != -1) {
       paging.setMaxId(mMaxId);
     }
     statuses = getStatuses(paging);
   } catch (final TwitterException e) {
     e.printStackTrace();
   }
   if (statuses != null) {
     Collections.sort(statuses, TWITTER4J_STATUS_ID_COMPARATOR);
     int deleted_count = 0;
     final int size = statuses.size();
     for (int i = 0; i < size; i++) {
       final Status status = statuses.get(i);
       if (deleteStatus(status.getId())) {
         deleted_count++;
       }
       data.add(
           new ParcelableStatus(
               status,
               account_id,
               i == statuses.size() - 1 ? deleted_count > 1 : false,
               isForceSSLConnection()));
     }
   }
   Collections.sort(data, ParcelableStatus.STATUS_ID_COMPARATOR);
   return data;
 }