Android Open Source - NewAndroidTwitter Twitter Request






From Project

Back to project page NewAndroidTwitter.

License

The source code is released under:

Apache License

If you think the Android project NewAndroidTwitter 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 net.londatiga.android.twitter;
//from   w w w  .  j  av a  2  s  .co m
import java.net.URL;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.AsyncTask;

import net.londatiga.android.twitter.oauth.OauthAccessToken;
import net.londatiga.android.twitter.oauth.OauthConsumer;

import net.londatiga.android.twitter.http.HttpConn;
import net.londatiga.android.twitter.util.Cons;

/**
 * Twitter API request.
 * 
 * @author Lorensius W. L. T <lorenz@londatiga.net>
 *
 */
public class TwitterRequest {
  private OauthConsumer mConsumer;
  private OauthAccessToken mAccessToken;
  
  public TwitterRequest(OauthConsumer consumer, OauthAccessToken accessToken) {
    mConsumer    = consumer;
    mAccessToken  = accessToken;
  }
  
  public void createRequest(String method, String url,  List<NameValuePair> params, RequestListener listener) {
    new RequestTask(method, url, params, listener).execute();
  }
  
  public void verifyCredentials(VerifyCredentialListener listener) {
    new VerifyCredentialTask(listener).execute();
  }
  
  private class RequestTask extends AsyncTask<URL, Integer, Long> {
    String method, url, response = "";
    
    List<NameValuePair> params;
    
    RequestListener listener;
    
    public RequestTask(String method, String url,  List<NameValuePair> params, RequestListener listener) {
      this.method    = method;
      this.url    = url;
      this.params    = params;
      this.listener   = listener;
    }
    
    protected void onCancelled() {
    }
    
      protected void onPreExecute() {
      }
    
        protected Long doInBackground(URL... urls) {         
            long result = 0;
            
            try {
               HttpConn conn   = new HttpConn(mConsumer, mAccessToken);
               
               if (method.equals(HttpConn.REQUEST_GET)) {
                 response  = conn.connectGet(url, params);
               } else {
                 response  = conn.connectPost(url, params);
               }
            } catch (Exception e) { 
              e.printStackTrace();
            }
            
            return result;
        }

        protected void onProgressUpdate(Integer... progress) {                
        }

        protected void onPostExecute(Long result) {
          if (!response.equals("")) {            
            listener.onSuccess(response);            
          } else {
            listener.onError("Failed to process api request");
          }
        }                
    }
  
  private class VerifyCredentialTask extends AsyncTask<URL, Integer, Long> {
    String response = "";
    
    VerifyCredentialListener listener;
    
    public VerifyCredentialTask(VerifyCredentialListener listener) {
      this.listener = listener;
    }
    
    protected void onCancelled() {
    }
    
      protected void onPreExecute() {
      }
    
        protected Long doInBackground(URL... urls) {         
            long result = 0;
            
            try {
               HttpConn conn   = new HttpConn(mConsumer, mAccessToken);
               
               response    = conn.connectGet(Cons.API_VERIFY_CREDENTIAL_URL, null);
            } catch (Exception e) { 
              e.printStackTrace();
            }
            
            return result;
        }

        protected void onProgressUpdate(Integer... progress) {                
        }

        protected void onPostExecute(Long result) {
          if (!response.equals("")) {
            try {
              JSONObject jsonObj     = (JSONObject) new JSONTokener(response).nextValue();
            
              TwitterUser user    = new TwitterUser();
              
              user.userId        = jsonObj.getString("id");
              user.screenName      = jsonObj.getString("screen_name");
              user.name        = jsonObj.getString("name");
              user.description    = jsonObj.getString("description");
              user.location      = jsonObj.getString("location");
              user.profileImageUrl  = jsonObj.getString("profile_image_url");
              user.website      = jsonObj.getString("url");
              user.followerCount    = jsonObj.getInt("followers_count");
              
              listener.onSuccess(user);
            } catch (Exception e) {
              listener.onError("Failed to get credentials");
            }
          } else {
            listener.onError("Failed to get credentials");
          }
        }                
    }
  
  public interface RequestListener {
    public abstract void onSuccess(String response);
    public abstract void onError(String error);
  }
  
  public interface VerifyCredentialListener {
    public abstract void onSuccess(TwitterUser user);
    public abstract void onError(String error);
  }
}




Java Source Code List

net.londatiga.android.example.BaseActivity.java
net.londatiga.android.example.MainActivity.java
net.londatiga.android.example.UserActivity.java
net.londatiga.android.twitter.TwitterDialog.java
net.londatiga.android.twitter.TwitterRequest.java
net.londatiga.android.twitter.TwitterUser.java
net.londatiga.android.twitter.Twitter.java
net.londatiga.android.twitter.http.HttpConn.java
net.londatiga.android.twitter.http.HttpParams.java
net.londatiga.android.twitter.http.HttpValues.java
net.londatiga.android.twitter.oauth.OauthAccessToken.java
net.londatiga.android.twitter.oauth.OauthConsumer.java
net.londatiga.android.twitter.oauth.OauthHeader.java
net.londatiga.android.twitter.oauth.OauthProvider.java
net.londatiga.android.twitter.oauth.OauthSignature.java
net.londatiga.android.twitter.oauth.OauthToken.java
net.londatiga.android.twitter.oauth.OauthUtil.java
net.londatiga.android.twitter.util.Base64.java
net.londatiga.android.twitter.util.Cons.java
net.londatiga.android.twitter.util.Debug.java
net.londatiga.android.twitter.util.StringUtil.java
net.londatiga.android.twitter.util.URIUtil.java