Android Open Source - PatternAndroid Rest Twitter






From Project

Back to project page PatternAndroid.

License

The source code is released under:

MIT License

If you think the Android project PatternAndroid 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.twitterclone.model.operations;
//  ww w  . j  a v a  2s .c o m
import java.util.ArrayList;
import java.util.HashMap;

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

import android.content.ContentValues;
import android.util.Base64;
import android.util.Log;

import com.foxykeep.datadroid.requestmanager.Request;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.twitterclone.model.provider.Contract.TwitterConnection;

public class RestTwitter {
  
  public static final ArrayList<ContentValues> tweetsValuesArray = new ArrayList<ContentValues>();
  
  public static void getTweets(Request request) {
    final String url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    final AsyncHttpClient client = new AsyncHttpClient();

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("screen_name", request.getString("screen_name"));
    final RequestParams tmp = new RequestParams(params);
    
    final AsyncHttpResponseHandler asyncHttpResponseHandler = new AsyncHttpResponseHandler() {

      public void onSuccess(String response) {
        try {
          JSONArray tweetsJson = new JSONArray(response);
          for (int i = 0; i < tweetsJson.length(); ++i) {
            ContentValues tweet = new ContentValues();
            tweet.put("user_name", tweetsJson.getJSONObject(i)
                .getJSONObject("user").getString("name"));
            tweet.put("body", tweetsJson.getJSONObject(i)
                .getString("text"));
            tweetsValuesArray.add(tweet);
            Log.e("", "JSON FILE " + " response " + tweet);
          }
        } catch (JSONException e) {
          Log.e("", "error " + e);
        }
      };

    };

    /*
     * OAuth Starts Here
     */
    RequestParams requestParams = new RequestParams();
    requestParams.put("grant_type", "client_credentials");
    AsyncHttpClient httpClient = new AsyncHttpClient();
    httpClient
        .addHeader(
            "Authorization",
            "Basic "
                + Base64.encodeToString(
                    (TwitterConnection.CONSUMER_KEY + ":" + TwitterConnection.CONSUMER_SECRET)
                        .getBytes(), Base64.NO_WRAP));
    httpClient.addHeader("Content-Type",
        "application/x-www-form-urlencoded;charset=UTF-8");
    httpClient.post("https://api.twitter.com/oauth2/token", requestParams,
        new AsyncHttpResponseHandler() {

          public void onSuccess(String responce) {

            try {
              JSONObject jsonObject = new JSONObject(responce);
              Log.e("",
                  "token_type "
                      + jsonObject
                          .getString("token_type")
                      + " access_token "
                      + jsonObject
                          .getString("access_token"));
              client.addHeader(
                  "Authorization",
                  jsonObject.getString("token_type")
                      + " "
                      + jsonObject
                          .getString("access_token"));
              client.get(url, tmp, asyncHttpResponseHandler);

            } catch (JSONException e) {
              e.printStackTrace();
            }
          };

          public void onFailure(Throwable error, String response) {
            Log.e("", "error " + error.toString() + " response "
                + response);
          };

        });
  }

}




Java Source Code List

android.support.util.Base64.java
android.support.util.LruCache.java
com.foxykeep.datadroid.exception.ConnectionException.java
com.foxykeep.datadroid.exception.CustomRequestException.java
com.foxykeep.datadroid.exception.DataException.java
com.foxykeep.datadroid.internal.network.NetworkConnectionImpl.java
com.foxykeep.datadroid.network.NetworkConnection.java
com.foxykeep.datadroid.network.UserAgentUtils.java
com.foxykeep.datadroid.requestmanager.RequestManager.java
com.foxykeep.datadroid.requestmanager.Request.java
com.foxykeep.datadroid.service.MultiThreadedIntentService.java
com.foxykeep.datadroid.service.RequestService.java
com.foxykeep.datadroid.util.DataDroidLog.java
com.foxykeep.datadroid.util.ObjectUtils.java
com.handmark.pulltorefresh.library.ILoadingLayout.java
com.handmark.pulltorefresh.library.IPullToRefresh.java
com.handmark.pulltorefresh.library.LoadingLayoutProxy.java
com.handmark.pulltorefresh.library.OverscrollHelper.java
com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.java
com.handmark.pulltorefresh.library.PullToRefreshBase.java
com.handmark.pulltorefresh.library.PullToRefreshExpandableListView.java
com.handmark.pulltorefresh.library.PullToRefreshGridView.java
com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshListView.java
com.handmark.pulltorefresh.library.PullToRefreshScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshWebView.java
com.handmark.pulltorefresh.library.extras.PullToRefreshWebView2.java
com.handmark.pulltorefresh.library.extras.SoundPullEventListener.java
com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor.java
com.handmark.pulltorefresh.library.internal.FlipLoadingLayout.java
com.handmark.pulltorefresh.library.internal.IndicatorLayout.java
com.handmark.pulltorefresh.library.internal.LoadingLayout.java
com.handmark.pulltorefresh.library.internal.RotateLoadingLayout.java
com.handmark.pulltorefresh.library.internal.Utils.java
com.handmark.pulltorefresh.library.internal.ViewCompat.java
com.twitterclone.model.RequestFactory.java
com.twitterclone.model.RestRequestManager.java
com.twitterclone.model.operations.RestTwitter.java
com.twitterclone.model.operations.TweetsOperation.java
com.twitterclone.model.provider.Contract.java
com.twitterclone.model.provider.RestProvider.java
com.twitterclone.model.service.RestService.java
com.twitterclone.ui.MainActivity.java