-1

This is my JSON response and i'm storing that response into Model Class

 JsonObjectRequest jsonbObjReq_parents_Child = new JsonObjectRequest(Request.Method.GET, "http://qnabubackend-env.2fuz4eh5jh.ap-south-1.elasticbeanstalk.com/parent/api/get/childs/"+user_id, null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response)
                {
                    try
                    {
                        Boolean error = response.getBoolean("error");
                        JSONArray jsonArray = response.getJSONArray("response");
                        int size = jsonArray.length();
                        Log.e("HEy>>>>",""+size);
                        for (int i=0;i<jsonArray.length();i++)
                        {
                            Log.e("H>>>>",""+size);
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            DataSet dataSet = new DataSet();
                            dataSet.setStudentId(jsonObject.getInt("id"));
                            dataSet.setStudentFirstName(jsonObject.getString("studentFirstName"));
                            dataSet.setStudentLastName(jsonObject.getString("studentLastName"));
                            dataSet.setStudentclassId(jsonObject.getInt("classId"));
                            dataSet.setStudentCurClass(jsonObject.getInt("currentClass"));
                            dataSet.setStudentCurClassSec(jsonObject.getString("currentClassSection"));
                            dataSet.setStudentMobileNum(jsonObject.getLong("phoneNumber"));
                            dataSet.setStudentSchoolId(jsonObject.getInt("schoolId"));
                            dataSet.setSessionYear(jsonObject.getString("sessionYear"));

                            JSONArray jsonArray1 = jsonObject.getJSONArray("subjects");

                            List<DataSet.SubjectsList> subjectsLists = new ArrayList<>();

                            int j;

                            for (j=0;j<jsonArray1.length();j++)
                            {
                                JSONObject jsonObject1 = jsonArray1.getJSONObject(j);
                                DataSet.SubjectsList dataSubjects = new DataSet.SubjectsList();
                                dataSubjects.setSubjectId(jsonObject1.getInt("id"));
                                dataSubjects.setSubjectsName(jsonObject1.getString("name"));
                                subjectsLists.add(dataSubjects);

                            }

                            dataSet.setSubjectsLists(subjectsLists);

                            childData.add(dataSet);

                        }

childData is my arraylist which is generic i.e.: DataSet.
Now I want to access data Like schoolId,sessionYear in another activity.

public class AnnualActivity {

JsonObjectRequest jsonbObjReq_parents_Child = new JsonObjectRequest(Request.Method.GET, "http://qnabubackend-env.2fuz4eh5jh.ap-south-1.elasticbeanstalk.com/school/api/annual/activity/get/list?schoolId="+schoolID+"&session="+sessionYear, null,

}  

this is not the next activity so i cannot use INTENTS Concept.

If i Do like This Im getting NullPointerException in AnnualActivity Class

 private List sharedPref(List<DataSet> data)
    {

        for (int i=0;i<data.size();i++)
        {
            DataSet dataSet = data.get(i);
            int a = dataSet.getStudentSchoolId();
            String b = dataSet.getSessionYear();

            Log.e("grrf",""+a);
            Log.e("fasfc",b);
        }

        return data;
    }
Phantômaxx
  • 37,352
  • 21
  • 80
  • 110

3 Answers3

1

You can store that ArrayList in sharedPreference like

  SharedPreferences preferences = Utils.getGCMPreferences(context);
     SharedPreferences.Editor editor = preferences.edit();
      editor.putString("key", new Gson().toJson(list));
       editor.commit();

list is your Model class type

and wherever you want to use that, you can get like

ModelClass model = null;
   SharedPreferences preferences = getGCMPreferences(context);
   String data = preferences.getString("key", null);

   model = new Gson().fromJson(data,ModelClass.class);
MaYur MahAjan
  • 133
  • 10
0

create method

private List sharedPref(List<DataSet> data)
{

    for (int i=0;i<data.size();i++)
    {
        DataSet dataSet = data.get(i);
        int a = dataSet.getStudentSchoolId();
        String b = dataSet.getSessionYear();
        getJsonRequest(a+"" , b);
        Log.e("grrf",""+a);
        Log.e("fasfc",b);
    }

    return data;
}

 public void getJsonRequest(String schoolID, String sessionYear){
    JsonObjectRequest jsonbObjReq_parents_Child = new JsonObjectRequest(Request.Method.GET, "http://qnabubackend-env.2fuz4eh5jh.ap-south-1.elasticbeanstalk.com/school/api/annual/activity/get/list?schoolId="+schoolID+"&session="+sessionYear, null);

}

call this method from loop and return the loop.

Jarvis
  • 1,544
  • 1
  • 20
  • 30
0

Have a simple method in your "api class"

public static void getChilds(final String user_id,  Response.Listener<JSONObject> res) {
    String baseUrl = "http://qnabubackend-env.2fuz4eh5jh.ap-south-1.elasticbeanstalk.com/parent/api";
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, baseUrl + "/get/childs/"+user_id, null,
        res);
    // TODO: add request to Volley queue
}

In your Activity, you will call

API.getChilds(id, new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                 // Parse the response here 
                 // Update your Activity data 
OneCricketeer
  • 151,199
  • 17
  • 111
  • 216