Search in sources :

Example 1 with SightSee

use of com.restart.spacestationtracker.data.SightSee in project Space-Station-Tracker by Kiarasht.

the class Locations method displayPasses.

/**
     * After successfully getting a city and country from the last JSON parsing, search a database
     * to see when ISS will pass by this city, country.
     */
public List<Date> displayPasses(final String latitude, final String longitude, final Context applicationContext) {
    // Used for Alert service
    final List<Date> passes = new ArrayList<>();
    final String url;
    if (latitude == null && longitude == null) {
        // Location.java is calling this method
        url = "http://api.open-notify.org/iss-pass.json?lat=" + mLatitude + "&lon=" + mLongitude + "&n=20";
    } else {
        // Alert.java is calling this method
        url = "http://api.open-notify.org/iss-pass.json?lat=" + latitude + "&lon=" + longitude;
    }
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                final JSONArray results = response.getJSONArray("response");
                final List<SightSee> dates = new ArrayList<>();
                for (int i = 0; i < results.length(); ++i) {
                    final JSONObject aPass = results.getJSONObject(i);
                    passes.add(new Date(Long.parseLong(aPass.getString("risetime")) * 1000L));
                    final SightSee aSightSee = new SightSee(aPass.getInt("duration"), aPass.getInt("risetime"));
                    dates.add(aSightSee);
                }
                final View headerView = LayoutInflater.from(mActivity).inflate(R.layout.locations_header, null);
                headerView.post(new Runnable() {

                    @Override
                    public void run() {
                        headerView.getLayoutParams().height = mFlexibleSpaceImageHeight;
                    }
                });
                mAdapter = new LocationAdapter(mActivity, headerView);
                mAdapter.setDataSet(dates);
                mRecyclerView.setAdapter(mAdapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError e) {
            if (latitude == null && longitude == null) {
                Toast.makeText(Locations.this, R.string.errorConnection, Toast.LENGTH_LONG).show();
            }
        }
    });
    // If Alert.java called us
    if (requestQueue == null) {
        RequestQueue requestQueue = Volley.newRequestQueue(applicationContext);
        requestQueue.add(jsonObjectRequest);
    } else {
        // If Locations.java called us
        jsonObjectRequest.setTag(TAG);
        requestQueue.add(jsonObjectRequest);
    }
    // Only Alert.java benefits from this return
    return passes;
}
Also used : VolleyError(com.android.volley.VolleyError) LocationAdapter(com.restart.spacestationtracker.adapter.LocationAdapter) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ImageView(android.widget.ImageView) ObservableRecyclerView(com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView) View(android.view.View) AdView(com.google.android.gms.ads.AdView) TextView(android.widget.TextView) Date(java.util.Date) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) SightSee(com.restart.spacestationtracker.data.SightSee) RequestQueue(com.android.volley.RequestQueue) ArrayList(java.util.ArrayList) List(java.util.List) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Aggregations

View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 RequestQueue (com.android.volley.RequestQueue)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 ObservableRecyclerView (com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView)1 AdView (com.google.android.gms.ads.AdView)1 LocationAdapter (com.restart.spacestationtracker.adapter.LocationAdapter)1 SightSee (com.restart.spacestationtracker.data.SightSee)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1