我试图在他/她登录后显示每个学生在listview中拍摄的主题。我使用共享首选项从登录页面引入用户。它在JSONParser上出错吗?
logcat错误。
03-30 18:02:26.457: I/System.out(28204): Response : User Found
03-30 18:02:26.715: D/Response:(28204): 123{"success":1,"student": [{"matrix_id":"123","ic_no":"123","name":"ALI","email":"ALI@yahoo.com"}]}
03-30 18:02:26.723: W/System.err(28204): org.json.JSONException: Value 123 of type java.lang.Integer cannot be converted to JSONObject
03-30 18:02:26.731: W/System.err(28204): at org.json.JSON.typeMismatch(JSON.java:111)
03-30 18:02:26.731: W/System.err(28204): at org.json.JSONObject.<init>(JSONObject.java:158)
03-30 18:02:26.731: W/System.err(28204): at org.json.JSONObject.<init>(JSONObject.java:171)
03-30 18:02:26.731: W/System.err(28204): at com.ultra.esc.ScheduleFragment$LoadAllSubject.doInBackground(ScheduleFragment.java:146)
03-30 18:02:26.731: W/System.err(28204): at com.ultra.esc.ScheduleFragment$LoadAllSubject.doInBackground(ScheduleFragment.java:1)
03-30 18:02:26.731: W/System.err(28204): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-30 18:02:26.731: W/System.err(28204): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-30 18:02:26.731: W/System.err(28204): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-30 18:02:26.731: W/System.err(28204): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-30 18:02:26.731: W/System.err(28204): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-30 18:02:26.731: W/System.err(28204): at java.lang.Thread.run(Thread.java:856)
这是登录后的代码。
//Bring the value from login page----------------------------------------------
SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
matrix_id = settings.getString("matrix","");
TextView matrix = (TextView)rootView.findViewById(R.id.textMatrix);
matrix.setText(settings.getString("matrix", "XXXXX"));
将值传递给JSON代码。
//protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// post matrix_id as GET parameters
params.add(new BasicNameValuePair("matrix_id", matrix_id));
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(url_all_subjects, "GET", params);
// Check your log cat for JSON reponse
Log.d("Response: ", json);
try {
JSONObject jObj = new JSONObject(json);
if(jObj != null){
String matrix_id = jObj.getString(TAG_MATRIX_ID);
subject = jObj.getJSONArray(TAG_SUCCESS);
if (subject != null) {
// looping through All Subjects
for (int i = 0; i < subject.length(); i++) {
JSONObject c = subject.getJSONObject(i);
// Storing each json item in variable
String subject = c.getString(TAG_SUBJECT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put("matrix_id", matrix_id);
map.put(TAG_SUBJECT, subject);
// adding HashList to ArrayList
subjectList.add(map);
}
}
}
else
{ Log.d("Subjects: ", "null");}
}
catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
ListAdapter adapter = new SimpleAdapter(
getActivity(), subjectList,
R.layout.all_subject, new String[] {TAG_MATRIX_ID,TAG_SUBJECT},
new int[] { R.id.matrix_id, R.id.name });
// updating listview
myListView.setAdapter(adapter);
}
答案 0 :(得分:1)
问题似乎是您将数组作为对象。试试这个:
SomeStudentModel model;
ArrayList<SomeStudentModel> typeList = new ArrayList<>();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
JSONObject jsonobject;
try {
JSONArray result = response.getJSONArray("student");
for (int i = 0; i < result.length(); i++) {
jsonobject = result.getJSONObject(i);
brand = gson.fromJson(jsonobject.toString(),
SomeStudentModel.class);
typeList.add(brand);
}
} catch (JSONException e) {
e.printStackTrace();
}
祝你好运!
答案 1 :(得分:0)
首先,我认为发布整个logcat错误是个好主意,正如@shayan pourvatan所说。
从您粘贴到帖子中的那一行判断,我认为将Integer
对象转换为JSONObject
时存在某种问题。我建议的不是将Integer
放入您的json,而是首先将其转换为String
并查看是否有帮助(例如Integer.toString(/*your number here*/)
。