Search in sources :

Example 1 with PeopleInSpaceAdapter

use of com.restart.spacestationtracker.adapter.PeopleInSpaceAdapter in project Space-Station-Tracker by Kiarasht.

the class PeopleinSpace method display_people.

/**
     * Displays a list of astronauts in a RecyclerView
     */
private void display_people() {
    final String url = "http://www.howmanypeopleareinspacerightnow.com/peopleinspace.json";
    final List<Astronaut> peopleInSpace = new ArrayList<>();
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
                mRecyclerView.setLayoutManager(layoutManager);
                mAdapter = new PeopleInSpaceAdapter(mActivity, peopleInSpace);
                mRecyclerView.setHasFixedSize(true);
                mRecyclerView.setNestedScrollingEnabled(true);
                mAdapter.setDataSet(peopleInSpace);
                mRecyclerView.setAdapter(mAdapter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError e) {
        }
    }) {

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
                JSONObject jsonResponse = new JSONObject(jsonString);
                try {
                    JSONArray astronauts = jsonResponse.getJSONArray("people");
                    for (int i = 0; i < astronauts.length(); ++i) {
                        JSONObject anAstronaut = astronauts.getJSONObject(i);
                        final String name = anAstronaut.getString("name");
                        final String image = anAstronaut.getString("biophoto");
                        final String countryLink = anAstronaut.getString("countryflag");
                        final String launchDate = anAstronaut.getString("launchdate");
                        String role = anAstronaut.getString("title");
                        final String location = anAstronaut.getString("location");
                        final String bio = anAstronaut.getString("bio");
                        final String wiki = anAstronaut.getString("biolink");
                        final String twitter = anAstronaut.getString("twitter");
                        if (role != null && !role.isEmpty())
                            role = role.substring(0, 1).toUpperCase() + role.substring(1);
                        Astronaut storeAnAstronaut = new Astronaut(name, image, countryLink, launchDate, role, location, bio, wiki, twitter);
                        peopleInSpace.add(storeAnAstronaut);
                    }
                    Collections.sort(peopleInSpace);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
    };
    mRequestQueue.add(jsonObjectRequest);
}
Also used : VolleyError(com.android.volley.VolleyError) Astronaut(com.restart.spacestationtracker.data.Astronaut) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PeopleInSpaceAdapter(com.restart.spacestationtracker.adapter.PeopleInSpaceAdapter) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) JSONObject(org.json.JSONObject) NetworkResponse(com.android.volley.NetworkResponse) ParseError(com.android.volley.ParseError) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Aggregations

LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 NetworkResponse (com.android.volley.NetworkResponse)1 ParseError (com.android.volley.ParseError)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 PeopleInSpaceAdapter (com.restart.spacestationtracker.adapter.PeopleInSpaceAdapter)1 Astronaut (com.restart.spacestationtracker.data.Astronaut)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1