mainActivity
package com.example.androidpractice; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.Iterator; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; public class MainActivity extends Activity { ArrayList<DateBean> sortArray = new ArrayList<DateBean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new FetchData().execute(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class FetchData extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { // Creating JSON Parser instance JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONObject json = jParser.getJSONFromUrl("your link"); try { JSONObject eventobject = json.getJSONObject("events"); Iterator<Object> keys = eventobject.keys(); while (keys.hasNext()) { String datestring = String.valueOf(keys.next()); // by below logic you skip missed date. if (datestring.trim().length() > 0) { SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd"); Date date = formatter.parse(datestring); DateBean dateBean = new DateBean(date); sortArray.add(dateBean); } else { } // JSONArray jsonArray = // eventobject.getJSONArray(datestring); // System.out.println(" --"+jsonArray); } System.out.println("size:" + sortArray.size()); System.out.println("==========sorting array======"); Collections.sort(sortArray, new CompareDate()); // reverse order // Collections.reverse(sortArray); for (DateBean d : sortArray) { String dateKey = new SimpleDateFormat("yyyy-MM-dd") .format(d.getDate()); System.out.println(dateKey); JSONArray jsonArray = eventobject.getJSONArray(dateKey); System.out.println(" --" + jsonArray); } // fetching location data JSONArray locationsobject = json.getJSONArray("locations"); String latitude = ""; String longitude = ""; String city = ""; String state = ""; String country = ""; String id = ""; String street = ""; String zip = ""; for (int i = 0; i < locationsobject.length(); i++) { JSONObject i_Loc = locationsobject.getJSONObject(i); System.out.println("location object:" + i_Loc); try { latitude = i_Loc.getString("latitude"); System.out.println("latitude :" + latitude); } catch (Exception e) { e.printStackTrace(); } try { longitude = i_Loc.getString("longitude"); System.out.println("longitude :" + longitude); } catch (Exception e) { e.printStackTrace(); } try { city = i_Loc.getString("city"); System.out.println("city name :" + city); } catch (Exception e) { e.printStackTrace(); } try { state = i_Loc.getString("state"); System.out.println("state :" + state); } catch (Exception e) { e.printStackTrace(); } try { country = i_Loc.getString("country"); System.out.println("country :" + country); } catch (Exception e) { e.printStackTrace(); } try { id = i_Loc.getString("id"); System.out.println("id :" + id); } catch (Exception e) { e.printStackTrace(); } try { street = i_Loc.getString("street"); System.out.println("street :" + street); } catch (Exception e) { e.printStackTrace(); } try { zip = i_Loc.getString("zip"); System.out.println("zip :" + zip); } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } class CompareDate implements Comparator<DateBean> { @Override public int compare(DateBean d1, DateBean d2) { return d1.getDate().compareTo(d2.getDate()); } } } }
No comments:
Post a Comment