How could I change the index value of a json object existing in my QueryUtils file from inside a Fragment?
This part right here: JSONObject currentDay = dayArray.getJSONObject(0);
I want the index value to change for each fragment but couldn't wrap my head around it. I have tried with intents and creating a constructor but failed.
As is right now, the app is 'working' with all fragments displaying the schedule for Monday (JSONObject index 0).
QueryUtils.java
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public final class QueryUtils {
    private static final String LOG_TAG = QueryUtils.class.getSimpleName();
    //makeHttpRequest constants
    private static final int READ_TIMEOUT = 10000 /* milliseconds */;
    private static final int CONNECT_TIMEOUT = 15000 /* milliseconds */;
    private static final int RESPONSE_CODE = 200 /*everything is OK*/;
    public QueryUtils() {
    }
    public static List<Day> fetchDayData(String requestUrl) {
        URL url = createUrl(requestUrl);
        String jsonResponse = null;
        try {
            jsonResponse = makeHttpRequest(url);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Problem making the HTTP request.", e);
        }
        List<Day> days = extractFeatureFromJson(jsonResponse);
        return days;
    }
    private static URL createUrl(String stringUrl) {
        URL url = null;
        try {
            url = new URL(stringUrl);
        } catch (MalformedURLException e) {
            Log.e(LOG_TAG, "Problem building the URL ", e);
        }
        return url;
    }
    private static String makeHttpRequest(URL url) throws IOException {
        String jsonResponse = "";
        if (url == null) {
            return jsonResponse;
        }
        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        try {
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(READ_TIMEOUT);
            urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();
            if (urlConnection.getResponseCode() == RESPONSE_CODE) {
                inputStream = urlConnection.getInputStream();
                jsonResponse = readFromStream(inputStream);
            } else {
                Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Problem retrieving Berceni JSON results.", e);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return jsonResponse;
    }
    private static String readFromStream(InputStream inputStream) throws IOException {
        StringBuilder output = new StringBuilder();
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = reader.readLine();
            while (line != null) {
                output.append(line);
                line = reader.readLine();
            }
        }
        return output.toString();
    }
    private static List<Day> extractFeatureFromJson(String dayJSON) {
        if (TextUtils.isEmpty(dayJSON)) {
            return null;
        }
        List<Day> days = new ArrayList<>();
        //Try to parse
        try {
            JSONObject baseJsonResponse = new JSONObject(dayJSON);
            JSONArray dayArray = baseJsonResponse.getJSONObject("schedule").getJSONArray("day");
            JSONObject currentDay = dayArray.getJSONObject(0);
            JSONArray getClasses = currentDay.getJSONArray("classes");
                for (int j = 0; j < getClasses.length(); j++) {
                    JSONObject currentClass = getClasses.getJSONObject(j);
                     String retrieveCourseTitle = currentClass.getString("class");
                     String retrieveCourseTime = currentClass.getString("time");
                     String retrieveCourseTrainer = currentClass.getString("trainer");
                     String retrieveCourseCancelState = currentClass.getString("canceled");
                     Day day = new Day(retrieveCourseTitle, retrieveCourseTime, retrieveCourseTrainer, retrieveCourseCancelState);
                     days.add(day);
                }
        } catch (JSONException e) {
            // If an error is thrown when executing any of the above statements in the "try" block,
            // catch the exception here, so the app doesn't crash. Print a log message
            // with the message from the exception.
            Log.e("QueryUtils", "Problem parsing JSON results", e);
        }
        return days;
    }
}
And my FragmentAdapter.java
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class FragmentAdapter extends FragmentPagerAdapter {
    private Context mContext;
    public FragmentAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }
    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return new MondayFragment();
        } else if (position == 1) {
            return new ThursdayFragment();
        } else if (position == 2) {
            return new WednesdayFragment();
        } else if (position == 3) {
            return new ThursdayFragment();
        } else if (position == 4) {
            return new FridayFragment();
        } else if (position == 5) {
            return new SaturdayFragment();
        } else {
            return new SundayFragment();
        }
    }
    /**
     * Return the total number of pages.
     */
    @Override
    public int getCount() {
        return 7;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return mContext.getString(R.string.monday);
        } else if (position == 1) {
            return mContext.getString(R.string.tuesday);
        } else if (position == 2) {
            return mContext.getString(R.string.wednesday);
        } else if (position == 3) {
            return mContext.getString(R.string.thursday);
        } else if (position == 4) {
            return mContext.getString(R.string.friday);
        } else if (position == 5) {
            return mContext.getString(R.string.saturday);
        }   else {
            return mContext.getString(R.string.sunday);
        }
    }
}
JSON Sample Response
{  
   "schedule":{
      "day":[
         {  
            "id":"Monday",
            "classes":[
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                },
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }
            ]
         }, 
         {  
            "id":"Tuesday",
            "classes":[
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                },
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }
            ]
         }, 
         {  
            "id":"Wednesday",
            "classes":[
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }, 
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }
            ]
         },
         {  
            "id":"Thursday",
            "classes":[
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                },
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }
            ]
         }, 
         {  
            "id":"Friday",
            "classes":[
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                },
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }
            ]
         },
         {  
            "id":"Saturday",
            "classes":[
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }, 
                {
                    "class" : "Class",
                    "time" : "00:00",
                    "trainer" : "Teacher",
                    "canceled" : ""
                }
            ]
         }, 
         {  
            "id":"Sunday",
            "classes":[]
         }
      ]
   }
}
 
     
    