Advertisement
skiddhard

SO Reference Code

Apr 23rd, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.60 KB | None | 0 0
  1. public class TestFeeds extends Activity {
  2.    
  3.     String apiResponse;
  4.     JSONObject JOFeeds;
  5.     JSONArray JAFeeds;
  6.    
  7.     ListView list;
  8.     LazyAdapter adapter;
  9.    
  10.     JSONObject json_data;
  11.    
  12.     String finalPictureFromObjectID;
  13.    
  14.     ArrayList<String> listOfURLS;
  15.    
  16.     String[] urlStrings;
  17.    
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.test_newsfeed_list);
  22.  
  23.         Bundle extras = getIntent().getExtras();
  24.         apiResponse = extras.getString("API_RESPONSE");
  25.        
  26.         try {
  27.             JOFeeds = new JSONObject(apiResponse);
  28. //          Log.v("DATA", JOFeeds.toString());
  29. //          JAFeeds = new JSONObject(apiResponse).getJSONArray("data");
  30. //          Log.e("DATA: ", JAFeeds.toString());
  31.            
  32.             JAFeeds = JOFeeds.getJSONArray("data");
  33.            
  34.             for (int i = 0; i < JAFeeds.length(); i++)  {
  35.                 json_data = JAFeeds.getJSONObject(i);
  36. //              Log.d("json_data", json_data.toString());
  37.                
  38.                
  39.                 // GET THE POST'S FROM USER NAME AND ID
  40.                 JSONObject joFromName = new JSONObject();
  41.                 joFromName = json_data.optJSONObject("from");
  42.                
  43.                 String fromName = joFromName.getString("name");
  44. //              Log.d("NAME:", fromName);
  45.                
  46.                
  47.                 // GET THE POST'S TO USER NAME AND ID
  48.                 if (json_data.has("to"))    {
  49.                    
  50.                     JSONObject joTo = json_data.optJSONObject("to");
  51.                    
  52.                     JSONArray arrayTo = null;
  53.                     try {
  54.                         arrayTo = joTo.getJSONArray("data");
  55.                     } catch (JSONException e)   {
  56.                         e.printStackTrace();
  57.                     }
  58.                    
  59.                     for(int t = 0; t < arrayTo.length(); t ++)  {
  60.                        
  61.                         try {
  62.                             JSONObject jsonDataTo = arrayTo.getJSONObject(t);
  63.                            
  64.                             String toName = jsonDataTo.getString("name");
  65. //                          Log.d("TO NAME", toName);
  66.                         } catch (JSONException e)   {
  67.                             e.printStackTrace();
  68.                         }
  69.                     }
  70.                 }
  71.                
  72.                
  73.                 // GET THE POST'S STORY / MESSAGE / NAME
  74.                 if (json_data.has("story")) {
  75.                     try {
  76.                         String story_message_name = json_data.getString("story");
  77. //                      Log.d("STORY", story_message_name);
  78.                     } catch (JSONException e) {
  79.                        
  80.                     }
  81.                    
  82.                 } else if (json_data.has("message")) {
  83.                     try {
  84.                         String story_message_name = json_data.getString("message");
  85. //                      Log.d("STORY", story_message_name);
  86.                     } catch (JSONException e) {
  87.                     }
  88.                 } else if (json_data.has("name")) {
  89.                     try {
  90.                         String story_message_name = json_data.getString("name");
  91. //                      Log.d("STORY", story_message_name);
  92.                     } catch (JSONException e) {
  93.                     }
  94.                 }
  95.                
  96.                
  97.                 // GET THE POST'S TIME STAMP
  98.                 if(json_data.has("created_time"))   {
  99.                    
  100.                     String dateStr = json_data.optString("created_time");
  101.                     SimpleDateFormat formatter = getDateFormat();
  102.                     ParsePosition pos = new ParsePosition(0);
  103.                     long then = formatter.parse(dateStr, pos).getTime();
  104.                     long now = new Date().getTime();
  105.  
  106.                     long seconds = (now - then)/1000;
  107.                     long minutes = seconds/60;
  108.                     long hours = minutes/60;
  109.                     long days = hours/24;
  110.  
  111.                     String friendly = null;
  112.                     long num = 0;
  113.                     if (days > 0) {
  114.                         num = days;
  115.                         friendly = days + " day";
  116.                     } else if (hours > 0) {
  117.                         num = hours;
  118.                         friendly = hours + " hour";
  119.                     } else if (minutes > 0) {
  120.                         num = minutes;
  121.                         friendly = minutes + " minute";
  122.                     } else {
  123.                         num = seconds;
  124.                         friendly = seconds + " second";
  125.                     }
  126.                     if (num > 1) {
  127.                         friendly += "s";
  128.                     }
  129.                     String postTimeStamp = friendly.toUpperCase() + " AGO";
  130. //                    Log.d("TIME STAMP", postTimeStamp);
  131.                 }
  132.                
  133.              // GET THE POST'S LIKES COUNT
  134.                 if (json_data.has("likes")) {
  135.                     JSONObject feedLikes = json_data.optJSONObject("likes");
  136.                    
  137.                     String countLikes = feedLikes.getString("count");
  138. //                  Log.d("COUNT LIKES", countLikes);
  139.                 } else {
  140.                     String countLikes = "0";
  141. //                  Log.d("COUNT LIKES", countLikes);
  142.                 }
  143.                
  144.              // GET THE POST'S COMMENTS COUNT
  145.                 if (json_data.has("comments"))  {
  146.                     JSONObject feedComments = json_data.optJSONObject("comments");
  147.                    
  148.                     String countComments = feedComments.getString("count");
  149. //                  Log.d("COUNT COMMENTS", countComments);
  150.                 } else {
  151.                     String countComments = "0";
  152. //                  Log.d("COUNT COMMENTS", countComments);
  153.                 }
  154.                
  155.              // GET THE POST'S PICTURES IF AVAILABLE
  156.                 if (json_data.has("picture"))   {
  157.                     try {
  158.                        
  159.                         String fetchedURL = null;
  160.                         URL imgValue = null;
  161.                         Bitmap userIcon = null;
  162.                         fetchedURL = json_data.getString("picture");
  163.                         String changedString = fetchedURL.replaceAll("_s.jpg", "_n.jpg");
  164.                         Log.v("PICTURE LINK URL :", changedString);
  165.                        
  166.                         imgValue = new URL(changedString);
  167.                         userIcon = BitmapFactory.decodeStream(imgValue.openConnection().getInputStream());
  168.                        
  169.                         for (int j = 0; j < json_data.length(); j++) {
  170.                            
  171.                             listOfURLS = new ArrayList<String>();
  172.                             listOfURLS.add(changedString);
  173.                            
  174.                         }
  175.                        
  176.                     }
  177.                     catch (JSONException e) {
  178.                     }
  179.                     catch (MalformedURLException e) {
  180.                         e.printStackTrace();
  181.                     }
  182.                     catch (IOException e)   {
  183.                         e.printStackTrace();
  184.                     }
  185.                 } else if (json_data.has("object_id")) {
  186.                     getPictureURL();
  187.                     listOfURLS = new ArrayList<String>();
  188.                     listOfURLS.add(finalPictureFromObjectID);
  189.                 }
  190.                
  191.              // GET THE POST'S DESCRIPTION
  192.                 if(json_data.has("description"))    {
  193.                     try {
  194.                         String description = json_data.getString("description");
  195. //                      Log.d("DESCRIPTION", description);
  196.                     } catch (JSONException e) {
  197.                        
  198.                     }
  199.                 }
  200.             }
  201.            
  202.         } catch (JSONException e) {
  203. //          showToast("Error: " + e.getMessage());
  204.             return;
  205.         }
  206.        
  207.         urlStrings = new String[listOfURLS.size()];
  208.         urlStrings = listOfURLS.toArray(urlStrings);
  209.         for (String s : urlStrings)
  210.             System.out.println(s);
  211.        
  212.         list = (ListView)findViewById(R.id.list);
  213.         adapter = new LazyAdapter(this, urlStrings);
  214.         list.setAdapter(adapter);
  215.     }
  216.    
  217.    
  218.     public static SimpleDateFormat getDateFormat() {
  219.         return new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
  220.     }
  221.  
  222.  
  223.     public void getPictureURL() {
  224.        
  225.         try {
  226.             String objectid;
  227.             objectid = json_data.getString("object_id");
  228. //          Log.v("NEW OBJ ID: ", objectid);
  229.            
  230. //          String query = "SELECT src_big FROM photo WHERE object_id = " + objectid;;
  231.             Bundle params = new Bundle();
  232.             params.putString("fields", "picture");
  233.             Utility.mAsyncRunner.request(objectid, params, new PhotoURLRequestListener());
  234.         }
  235.         catch (JSONException e) {
  236.             e.printStackTrace();
  237.         }
  238.     }
  239.    
  240.    
  241.     public class PhotoURLRequestListener extends BaseRequestListener {
  242.  
  243.         @Override
  244.         public void onComplete(final String response, final Object state) {
  245.            
  246.             String json = response;
  247.             try {
  248.                 JSONObject jsonPictureFromObjectID = new JSONObject(json);
  249. //              Log.d("PICTURE: ", jsonPictureFromObjectID.getString("picture"));
  250. //              Log.d("ORIGINAL: ", jsonPictureFromObjectID.toString());
  251.                 finalPictureFromObjectID = jsonPictureFromObjectID.getString("picture");
  252. //              Log.d("FEED PIC FROM OBJ_ID URL: ", finalPictureFromObjectID);
  253.             }
  254.             catch (JSONException e) {
  255.                
  256.             }
  257.         }
  258.     }
  259.    
  260.    
  261.     private String[] mStrings = {
  262.             "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
  263.             "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
  264.             "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
  265.             "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
  266.             "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
  267.             "http://a1.twimg.com/profile_images/655119538/andbook.png"
  268.     };
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement