【发布时间】:2018-01-27 08:59:34
【问题描述】:
这是我的 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 是我的通用数组列表,即: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,
}
这不是下一个活动,所以我不能使用意图概念。
如果我这样做,我会在 AnnualActivity 类中得到 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;
}
【问题讨论】:
-
你想从哪个类调用上面的 url
-
年度活动课程。我想从模型类中获取数据要访问这里本身以及我需要在 URL 中传递的数据
-
为什么你不能使用 intent.putExtra ?并在 AnnulActivity 类中处理数据
-
你能从你要调用的类中访问 childData 数组列表
-
在我得到响应的活动和我想在 URL 中传递参数的活动之间,我有另一个活动。
标签: java android arraylist nullpointerexception