这是我的JSON响应,我将该响应存储到模型类
中 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是我的arraylist,它是通用的,即:DataSet 现在我想在另一个活动中访问像schoolId,sessionYear这样的数据。
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,
}
这不是下一个活动所以我不能使用INTENTS Concept。
如果我喜欢这个我在AnnualActivity Class中得到NullPointerException
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;
}
答案 0 :(得分:0)
创建方法
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);
}
从循环调用此方法并返回循环。
答案 1 :(得分:0)
您可以将该ArrayList存储在sharedPreference中,如
SharedPreferences preferences = Utils.getGCMPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("key", new Gson().toJson(list));
editor.commit();
list是您的Model类类型
无论你想在哪里使用它,都可以像
一样ModelClass model = null;
SharedPreferences preferences = getGCMPreferences(context);
String data = preferences.getString("key", null);
model = new Gson().fromJson(data,ModelClass.class);
答案 2 :(得分:0)
在“api课程”中有一个简单的方法
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
}
在您的活动中,您将致电
API.getChilds(id, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
// Parse the response here
// Update your Activity data