Back to project page PatternAndroid.
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.
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); }; }); } }