-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,901
  • 21
  • 84
  • 115
Kamal
  • 45
  • 1
  • 10
  • from which class you want call above url – Jarvis Jan 27 '18 at 12:15
  • AnnualAcitvity Class. I want to get Data From Model Class To access here itself and that data i need to pass in URL as I mentioned – Kamal Jan 27 '18 at 12:30
  • Why you cant use intent.putExtra ? And process the data in AnnulActivity class – Jarvis Jan 27 '18 at 12:33
  • Are you able to access childData arraylist from the class you are going to call AnnualActivity – Jarvis Jan 27 '18 at 12:35
  • In between the Activity which im getting response and the activity which i want pass parameters in URL, I have Another Activity. – Kamal Jan 27 '18 at 12:37
  • You want to call that url from all the data in Arraylist? – Jarvis Jan 27 '18 at 12:39
  • Not All The Data From ArrayList, Only schoolID, and SessionYear. but when i create ArrayList childData = new ArrayList<>() and childData.get() Method in AnnualActivity Class, index postion i dont know and when i use For Loop, i'm unable to access that data outside for Loop – Kamal Jan 27 '18 at 12:46
  • you pass that data in another method and return the for loop, if you got your data using return; – Jarvis Jan 27 '18 at 12:51
  • I Just Edited My Question, U Want Me To do Like That, But Im Getting NullPointerException – Kamal Jan 27 '18 at 13:38
  • Please include the logcat as well, then – OneCricketeer Jan 27 '18 at 13:52
  • See https://stackoverflow.com/questions/10996479/how-to-update-a-textview-of-an-activity-from-another-class/47637313#47637313 – EpicPandaForce Jan 27 '18 at 14:10

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
  • 143
  • 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,714
  • 2
  • 20
  • 35
  • That is not activity bro, it is a model class(POJO Class) and i need to access that data in other activity. and that data i want to put in API(URL). – Kamal Jan 27 '18 at 10:00
  • to access that in other activity you have pass the data from from intent. or that object should publicly accessible. – Jarvis Jan 27 '18 at 10:06
  • ok I understand. but when i get data in ArrayList how can i extract data from ArrayList beacause URL will not take ArrayList as input. – Kamal Jan 27 '18 at 10:15
  • if you need pass whole array data then convert in to json and send that data – Jarvis Jan 27 '18 at 10:41
  • or create url using model class data as per your requirement – Jarvis Jan 27 '18 at 10:43
  • bro, give me some example so that i can look into that code. or if u want me to post my code, i'll do. – Kamal Jan 27 '18 at 11:37
  • Ok let me know which url you want to create using which variables of model and and position on model in arraylist – Jarvis Jan 27 '18 at 11:43
  • Ok bro. I'll Edit My Question? – Kamal Jan 27 '18 at 11:45
  • I Edited My Question – Kamal Jan 27 '18 at 12:06
  • Try to make your child data accessible in another class. retrieving data from childData is just you have make accessible. currently i dont have whole from where you want to pass this data. – Jarvis Jan 27 '18 at 12:23
  • Still It is Not Working, i'll Show You What I did. I edited My Question – Kamal Jan 27 '18 at 14:35
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
  • 179,855
  • 19
  • 132
  • 245