Android Open Source - lastUpdates Async Twitter Parser






From Project

Back to project page lastUpdates.

License

The source code is released under:

GNU General Public License

If you think the Android project lastUpdates listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.focusings.focusingsworld.TwitterParser;
//from  ww  w  . j a  v a  2s.com
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.focusings.focusingsworld.MainActivity;
import com.focusings.focusingsworld.R;
import com.focusings.focusingsworld.pullToRefreshLibrary.PullToRefreshListView;
import com.focusings.focusingsworld.pullToRefreshLibrary.PullToRefreshTwitterOnRefreshListener;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

public class AsyncTwitterParser extends AsyncTask<String, Integer,List<TweetInfo>> {

  private MainActivity mainActivity;
  private boolean needToCallOnRefreshComplete=false;
  
  @Override
  protected void onPreExecute() {
    super.onPreExecute();
  }
  
  public AsyncTwitterParser(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }
  
  @Override
  protected List<TweetInfo> doInBackground(String... params) {
    List<TweetInfo> tweets=new ArrayList<TweetInfo>();
  
    if (params[0].equals("true")){
      needToCallOnRefreshComplete=true;
    }
    
    //Get tweets info
    try {
      String timeLineUser=TwitterUtils.getTimelineForUser();
      JSONArray jsonArray = new JSONArray(timeLineUser);
      JSONObject jsonObject;

      for (int i = 0; i < jsonArray.length(); i++) {
        
        jsonObject = (JSONObject) jsonArray.get(i);
        TweetInfo currentTweet = new TweetInfo();

        //Case retweet I need to access to the data in retweeted_status
        if (jsonObject.has("retweeted_status")){
          jsonObject=jsonObject.getJSONObject("retweeted_status");
        }
        
        currentTweet.setTweetContent(jsonObject.getString("text"));
        currentTweet.setUserImage(jsonObject.getJSONObject("user").getString("profile_image_url"));
        currentTweet.setUserName(jsonObject.getJSONObject("user").getString("name"));
        currentTweet.setUserTwitterAccount("@"+jsonObject.getJSONObject("user").getString("screen_name"));
              
        tweets.add(i, currentTweet);
      }

    } catch (Exception e) {
      Log.e("JSON error: ", Log.getStackTraceString(e));        
    }
    return tweets;
  }
  
  @Override
  protected void onPostExecute(List<TweetInfo> tweets) { 
    //this you will received result fired from async class of onPostExecute(result) method.
    TweetsListAdapter adapter = new TweetsListAdapter(mainActivity,tweets);
    
    TextView loadingTwitterView=(TextView)mainActivity.findViewById(R.id.loadingTwitter);
    loadingTwitterView.setText("");
    
    TextView errorLog=null;
    PullToRefreshListView listView=null;
    errorLog=(TextView)mainActivity.findViewById(R.id.errorLogTwitter);
    listView = (PullToRefreshListView)mainActivity.findViewById(R.id.pull_to_refresh_listview_twitter);
    
    if (tweets.size()==0){
      if (mainActivity.isOnline()==false){        
        errorLog.setText(R.string.noInternetConnection);
      }else{
        errorLog.setText(R.string.noTwitterConnection);
      }
    }else{
      errorLog.setText("");
    }
    
    //According to the pullToRefresh library I'm using, I need to call listView.onRefreshComplete() 
    //so that the spinner stops, but I only need to do this if I come from onRefreshListener (because it
    //means the spinner was already been displayed), if I come from initializations the spinner isn't displayed,
    //so, I shouldn't call listView.onRefreshComplete()
    if (needToCallOnRefreshComplete && listView!=null){
      listView.onRefreshComplete();
    }
    
    //Case of listView
    if (listView!=null){
      listView.setOnRefreshListener(new PullToRefreshTwitterOnRefreshListener(mainActivity));    
      listView.setAdapter(adapter);
    }  
  }
}




Java Source Code List

com.focusings.focusingsworld.MainActivity.java
com.focusings.focusingsworld.VideoInfo.java
com.focusings.focusingsworld.ImageAndTextList.AsyncImageLoader.java
com.focusings.focusingsworld.ImageAndTextList.ImageAndTextListAdapter.java
com.focusings.focusingsworld.ImageAndTextList.ImageAndText.java
com.focusings.focusingsworld.ImageAndTextList.ShareVideoOnItemClickListener.java
com.focusings.focusingsworld.ImageAndTextList.ViewCache.java
com.focusings.focusingsworld.ImageAndTextList.ViewVideoOnItemClickListener.java
com.focusings.focusingsworld.TwitterParser.AsyncTwitterParser.java
com.focusings.focusingsworld.TwitterParser.TweetInfo.java
com.focusings.focusingsworld.TwitterParser.TweetsListAdapter.java
com.focusings.focusingsworld.TwitterParser.TwitterUtils.java
com.focusings.focusingsworld.YoutubeParser.AsyncYoutubeParser.java
com.focusings.focusingsworld.notificationManagement.AsyncNotificationResponse.java
com.focusings.focusingsworld.notificationManagement.CheckNewUpdatesServiceReceiver.java
com.focusings.focusingsworld.notificationManagement.CheckNewUpdatesService.java
com.focusings.focusingsworld.notificationManagement.Update.java
com.focusings.focusingsworld.pullToRefreshLibrary.PullToRefreshListView.java
com.focusings.focusingsworld.pullToRefreshLibrary.PullToRefreshTwitterOnRefreshListener.java
com.focusings.focusingsworld.pullToRefreshLibrary.PullToRefreshYoutubeOnRefreshListener.java
com.focusings.focusingsworld.shop.GoToStaffWebsiteOnClickListener.java