Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestFeeds extends Activity {
- String apiResponse;
- JSONObject JOFeeds;
- JSONArray JAFeeds;
- ListView list;
- LazyAdapter adapter;
- JSONObject json_data;
- String finalPictureFromObjectID;
- ArrayList<String> listOfURLS;
- String[] urlStrings;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test_newsfeed_list);
- Bundle extras = getIntent().getExtras();
- apiResponse = extras.getString("API_RESPONSE");
- try {
- JOFeeds = new JSONObject(apiResponse);
- // Log.v("DATA", JOFeeds.toString());
- // JAFeeds = new JSONObject(apiResponse).getJSONArray("data");
- // Log.e("DATA: ", JAFeeds.toString());
- JAFeeds = JOFeeds.getJSONArray("data");
- for (int i = 0; i < JAFeeds.length(); i++) {
- json_data = JAFeeds.getJSONObject(i);
- // Log.d("json_data", json_data.toString());
- // GET THE POST'S FROM USER NAME AND ID
- JSONObject joFromName = new JSONObject();
- joFromName = json_data.optJSONObject("from");
- String fromName = joFromName.getString("name");
- // Log.d("NAME:", fromName);
- // GET THE POST'S TO USER NAME AND ID
- if (json_data.has("to")) {
- JSONObject joTo = json_data.optJSONObject("to");
- JSONArray arrayTo = null;
- try {
- arrayTo = joTo.getJSONArray("data");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- for(int t = 0; t < arrayTo.length(); t ++) {
- try {
- JSONObject jsonDataTo = arrayTo.getJSONObject(t);
- String toName = jsonDataTo.getString("name");
- // Log.d("TO NAME", toName);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- // GET THE POST'S STORY / MESSAGE / NAME
- if (json_data.has("story")) {
- try {
- String story_message_name = json_data.getString("story");
- // Log.d("STORY", story_message_name);
- } catch (JSONException e) {
- }
- } else if (json_data.has("message")) {
- try {
- String story_message_name = json_data.getString("message");
- // Log.d("STORY", story_message_name);
- } catch (JSONException e) {
- }
- } else if (json_data.has("name")) {
- try {
- String story_message_name = json_data.getString("name");
- // Log.d("STORY", story_message_name);
- } catch (JSONException e) {
- }
- }
- // GET THE POST'S TIME STAMP
- if(json_data.has("created_time")) {
- String dateStr = json_data.optString("created_time");
- SimpleDateFormat formatter = getDateFormat();
- ParsePosition pos = new ParsePosition(0);
- long then = formatter.parse(dateStr, pos).getTime();
- long now = new Date().getTime();
- long seconds = (now - then)/1000;
- long minutes = seconds/60;
- long hours = minutes/60;
- long days = hours/24;
- String friendly = null;
- long num = 0;
- if (days > 0) {
- num = days;
- friendly = days + " day";
- } else if (hours > 0) {
- num = hours;
- friendly = hours + " hour";
- } else if (minutes > 0) {
- num = minutes;
- friendly = minutes + " minute";
- } else {
- num = seconds;
- friendly = seconds + " second";
- }
- if (num > 1) {
- friendly += "s";
- }
- String postTimeStamp = friendly.toUpperCase() + " AGO";
- // Log.d("TIME STAMP", postTimeStamp);
- }
- // GET THE POST'S LIKES COUNT
- if (json_data.has("likes")) {
- JSONObject feedLikes = json_data.optJSONObject("likes");
- String countLikes = feedLikes.getString("count");
- // Log.d("COUNT LIKES", countLikes);
- } else {
- String countLikes = "0";
- // Log.d("COUNT LIKES", countLikes);
- }
- // GET THE POST'S COMMENTS COUNT
- if (json_data.has("comments")) {
- JSONObject feedComments = json_data.optJSONObject("comments");
- String countComments = feedComments.getString("count");
- // Log.d("COUNT COMMENTS", countComments);
- } else {
- String countComments = "0";
- // Log.d("COUNT COMMENTS", countComments);
- }
- // GET THE POST'S PICTURES IF AVAILABLE
- if (json_data.has("picture")) {
- try {
- String fetchedURL = null;
- URL imgValue = null;
- Bitmap userIcon = null;
- fetchedURL = json_data.getString("picture");
- String changedString = fetchedURL.replaceAll("_s.jpg", "_n.jpg");
- Log.v("PICTURE LINK URL :", changedString);
- imgValue = new URL(changedString);
- userIcon = BitmapFactory.decodeStream(imgValue.openConnection().getInputStream());
- for (int j = 0; j < json_data.length(); j++) {
- listOfURLS = new ArrayList<String>();
- listOfURLS.add(changedString);
- }
- }
- catch (JSONException e) {
- }
- catch (MalformedURLException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- } else if (json_data.has("object_id")) {
- getPictureURL();
- listOfURLS = new ArrayList<String>();
- listOfURLS.add(finalPictureFromObjectID);
- }
- // GET THE POST'S DESCRIPTION
- if(json_data.has("description")) {
- try {
- String description = json_data.getString("description");
- // Log.d("DESCRIPTION", description);
- } catch (JSONException e) {
- }
- }
- }
- } catch (JSONException e) {
- // showToast("Error: " + e.getMessage());
- return;
- }
- urlStrings = new String[listOfURLS.size()];
- urlStrings = listOfURLS.toArray(urlStrings);
- for (String s : urlStrings)
- System.out.println(s);
- list = (ListView)findViewById(R.id.list);
- adapter = new LazyAdapter(this, urlStrings);
- list.setAdapter(adapter);
- }
- public static SimpleDateFormat getDateFormat() {
- return new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
- }
- public void getPictureURL() {
- try {
- String objectid;
- objectid = json_data.getString("object_id");
- // Log.v("NEW OBJ ID: ", objectid);
- // String query = "SELECT src_big FROM photo WHERE object_id = " + objectid;;
- Bundle params = new Bundle();
- params.putString("fields", "picture");
- Utility.mAsyncRunner.request(objectid, params, new PhotoURLRequestListener());
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public class PhotoURLRequestListener extends BaseRequestListener {
- @Override
- public void onComplete(final String response, final Object state) {
- String json = response;
- try {
- JSONObject jsonPictureFromObjectID = new JSONObject(json);
- // Log.d("PICTURE: ", jsonPictureFromObjectID.getString("picture"));
- // Log.d("ORIGINAL: ", jsonPictureFromObjectID.toString());
- finalPictureFromObjectID = jsonPictureFromObjectID.getString("picture");
- // Log.d("FEED PIC FROM OBJ_ID URL: ", finalPictureFromObjectID);
- }
- catch (JSONException e) {
- }
- }
- }
- private String[] mStrings = {
- "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
- "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
- "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
- "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
- "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
- "http://a1.twimg.com/profile_images/655119538/andbook.png"
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement