Android/app/src/main/java/io/github/project_travel_mate/destinations/CityFragment.java
private void fetchCitiesList() {

        // to fetch 6 city names
        String uri = API_LINK_V2 + "get-all-cities/10";
        Log.v("EXECUTING", uri);

        //Set up client
        OkHttpClient client = new OkHttpClient();
        //Execute request
        final Request request = new Request.Builder()
                .header("Authorization", "Token " + mToken)
                .url(uri)
                .build();
        //Setup callback
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("Request Failed", "Message : " + e.getMessage());
                mHandler.post(() -> networkError());
            }

            @Override
            public void onResponse(Call call, final Response response) {
                mHandler.post(() -> {
                    if (response.isSuccessful()) {
                        try {
                            String res = response.body().string();
                            Log.v("RESULT", res);
                            animationView.setVisibility(View.GONE);
                            JSONArray ar = new JSONArray(res);
                            FlipSettings settings = new FlipSettings.Builder().defaultPage().build();
                            List<City> cities = new ArrayList<>();
                            for (int i = 0; i < ar.length(); i++) {
                                City city = new City(
                                        ar.getJSONObject(i).getString("id"),
                                        ar.getJSONObject(i).optString("image"),
                                        ar.getJSONObject(i).getString("city_name"),
                                        ar.getJSONObject(i).getInt("facts_count"),
                                        mColors[i],
                                        mActivity.getApplicationContext().getString(R.string.interest_know_more),
                                        mActivity.getApplicationContext().getString(R.string.interest_weather),
                                        mActivity.getApplicationContext().getString(R.string.interest_fun_facts),
                                        mActivity.getApplicationContext().getString(R.string.interest_trends));
                                cities.add(city);

                                //Inset into local DB
                                mDatabase.cityDao().insert(city);
                                SharedPreferences.Editor editor = mSharedPreferences.edit();
                                editor.putString(LAST_CACHE_TIME, mFormat.format(new Date()).toString());
                                editor.apply();

                            }

                            lv.setAdapter(new CityAdapter(mActivity, cities, settings));
                            lv.setOnItemClickListener((parent, view, position, id1) -> {
                                City city = (City) lv.getAdapter().getItem(position);
                                Intent intent = FinalCityInfoActivity.getStartIntent(mActivity, city);
                                startActivity(intent);
                            });

                        } catch (JSONException | IOException e) {
                            e.printStackTrace();
                            Log.e("ERROR", "Message : " + e.getMessage());
                            networkError();
                        }
                    } else {
                        networkError();
                    }
                });
            }
        });
    }
/**
     * Fetches the list of popular cities from server
     */
Fetches the list of popular cities from server
Fetches the list of popular cities from server
private void fetchCitiesList() {
        String uri = API_LINK_V2 + "get-all-cities/10";
        Log.v("EXECUTING", uri);
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder()
                .header("Authorization", "Token " + mToken)
                .url(uri)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("Request Failed", "Message : " + e.getMessage());
                mHandler.post(() -> networkError());
            }
            @Override
            public void onResponse(Call call, final Response response) {
                mHandler.post(() -> {
                    if (response.isSuccessful()) {
                        try {
                            String res = response.body().string();
                            Log.v("RESULT", res);
                            animationView.setVisibility(View.GONE);
                            JSONArray ar = new JSONArray(res);
                            FlipSettings settings = new FlipSettings.Builder().defaultPage().build();
                            List<City> cities = new ArrayList<>();
                            for (int i = 0; i < ar.length(); i++) {
                                City city = new City(
                                        ar.getJSONObject(i).getString("id"),
                                        ar.getJSONObject(i).optString("image"),
                                        ar.getJSONObject(i).getString("city_name"),
                                        ar.getJSONObject(i).getInt("facts_count"),
                                        mColors[i],
                                        mActivity.getApplicationContext().getString(R.string.interest_know_more),
                                        mActivity.getApplicationContext().getString(R.string.interest_weather),
                                        mActivity.getApplicationContext().getString(R.string.interest_fun_facts),
                                        mActivity.getApplicationContext().getString(R.string.interest_trends));
                                cities.add(city);
                                mDatabase.cityDao().insert(city);
                                SharedPreferences.Editor editor = mSharedPreferences.edit();
                                editor.putString(LAST_CACHE_TIME, mFormat.format(new Date()).toString());
                                editor.apply();
                            }
                            lv.setAdapter(new CityAdapter(mActivity, cities, settings));
                            lv.setOnItemClickListener((parent, view, position, id1) -> {
                                City city = (City) lv.getAdapter().getItem(position);
                                Intent intent = FinalCityInfoActivity.getStartIntent(mActivity, city);
                                startActivity(intent);
                            });
                        } catch (JSONException | IOException e) {
                            e.printStackTrace();
                            Log.e("ERROR", "Message : " + e.getMessage());
                            networkError();
                        }
                    } else {
                        networkError();
                    }
                });
            }
        });
    }
private void fetchCitiesList ( ) { String uri = API_LINK_V2 + "get-all-cities/10" ; Log . v ( "EXECUTING" , uri ) ; OkHttpClient client = new OkHttpClient ( ) ; final Request request = new Request . Builder ( ) . header ( "Authorization" , "Token " + mToken ) . url ( uri ) . build ( ) ; client . newCall ( request ) . enqueue ( new Callback ( ) { @ Override public void onFailure ( Call call , IOException e ) { Log . e ( "Request Failed" , "Message : " + e . getMessage ( ) ) ; mHandler . post ( ( ) -> networkError ( ) ) ; } @ Override public void onResponse ( Call call , final Response response ) { mHandler . post ( ( ) -> { if ( response . isSuccessful ( ) ) { try { String res = response . body ( ) . string ( ) ; Log . v ( "RESULT" , res ) ; animationView . setVisibility ( View . GONE ) ; JSONArray ar = new JSONArray ( res ) ; FlipSettings settings = new FlipSettings . Builder ( ) . defaultPage ( ) . build ( ) ; List < City > cities = new ArrayList < > ( ) ; for ( int i = 0 ; i < ar . length ( ) ; i ++ ) { City city = new City ( ar . getJSONObject ( i ) . getString ( "id" ) , ar . getJSONObject ( i ) . optString ( "image" ) , ar . getJSONObject ( i ) . getString ( "city_name" ) , ar . getJSONObject ( i ) . getInt ( "facts_count" ) , mColors [ i ] , mActivity . getApplicationContext ( ) . getString ( R . string . interest_know_more ) , mActivity . getApplicationContext ( ) . getString ( R . string . interest_weather ) , mActivity . getApplicationContext ( ) . getString ( R . string . interest_fun_facts ) , mActivity . getApplicationContext ( ) . getString ( R . string . interest_trends ) ) ; cities . add ( city ) ; mDatabase . cityDao ( ) . insert ( city ) ; SharedPreferences . Editor editor = mSharedPreferences . edit ( ) ; editor . putString ( LAST_CACHE_TIME , mFormat . format ( new Date ( ) ) . toString ( ) ) ; editor . apply ( ) ; } lv . setAdapter ( new CityAdapter ( mActivity , cities , settings ) ) ; lv . setOnItemClickListener ( ( parent , view , position , id1 ) -> { City city = ( City ) lv . getAdapter ( ) . getItem ( position ) ; Intent intent = FinalCityInfoActivity . getStartIntent ( mActivity , city ) ; startActivity ( intent ) ; } ) ; } catch ( JSONException | IOException e ) { e . printStackTrace ( ) ; Log . e ( "ERROR" , "Message : " + e . getMessage ( ) ) ; networkError ( ) ; } } else { networkError ( ) ; } } ) ; } } ) ; }
Fetches the list of popular cities from server
Fetches the list of popular cities from server
// to fetch 6 city names //Set up client //Execute request //Setup callback //Inset into local DB