2014-03-30 95 views
0

我試圖顯示一旦他/她登錄,每個學生在listview中採取的主題。我使用共享首選項從登錄頁面中引導用戶。 JSONParser有錯嗎?JSONException:java.lang.Integer無法轉換爲JSONObject`

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":"[email protected]"}]} 
    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

請張貼所有logcat的錯誤,是的,它是在解析 –

+0

@shayanpourvatan我張貼的logcat錯誤 – Dendi

+0

'123 { 「成功」:1, 「學生」:[{ 「matrix_id」:「123」,「ic_no」:「123」,「name」:「ALI」,「email」:「[email protected]」}]}':是最初的** 123 ** 「{」)ap你的迴應藝術也是如此..? – Swayam

回答

0

首先,我認爲這是一個好主意,作爲@shayan pourvatan說,發佈整個logcat錯誤。

從您粘貼到您的文章中的1行來判斷,我認爲在將Integer對象轉換爲JSONObject時存在某種問題。什麼我建議是不是把Integer到您的JSON,將其轉換爲String第一,看看是否有幫助(即Integer.toString(/*your number here*/)

+0

我發佈了logcat錯誤先生。 – Dendi

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(); 
     } 

好運

相關問題