2014-07-14 7 views
0

메시지 없음 값은된 JSONObject 예외 : 나는 안드로이드 응용 프로그램을 개발하는이 익숙한 예외가 발생

메시지에 대해 어떤 값은

그리고 난 확신 내가 가지고있는 된 JSONObject 내가 찾고있어 무엇을 포함하지 않는다 대한 : 여기

내 JSON 개체이며, 당신은 더 나은 여기 이해를 포맷 할 수 있습니다 http://jsonformatter.curiousconcept.com/

{ 
    "data": [ 
    { 
     "id": "1438734076389483_1440132306249660", 
     "from": { 
     "category": "Community", 
     "name": "AKA", 
     "id": "14387376389483" 
     }, 
     "message": "TEST TEST", 
     "actions": [ 
     { 
      "name": "Comment", 
      "link": "https://www.facebook.com/14387340769483/posts/14401323069660" 
     }, 
     { 
      "name": "Like", 
      "link": "https://www.facebook.com/14387376389483/posts/14402306249660" 
     } 
     ], 
     "privacy": { 
     "description": "Public", 
     "value": "EVERYONE", 
     "friends": "", 
     "networks": "", 
     "allow": "", 
     "deny": "" 
     }, 
     "type": "status", 
     "status_type": "mobile_status_update", 
     "created_time": "2014-07-14T18:49:43+0000", 
     "updated_time": "2014-07-14T18:49:43+0000", 
     "likes": { 
     "data": [ 
      { 
      "id": "84207420", 
      "name": "Akira Yuki" 
      }, 
      { 
      "id": "14387346389483", 
      "name": "AKA" 
      } 
     ], 
     "paging": { 
      "cursors": { 
      "after": "MTQzODczNDA3NjM4OTQ4Mw==", 
      "before": "ODQyMDQ3NDIw" 
      } 
     } 
     } 
    }, 
    { 
     "id": "1438734076389483_14399988596342", 
     "from": { 
     "category": "Community", 
     "name": "AKA", 
     "id": "1438734076389483" 
     }, 
     "message": "MEssage test number twooooo", 
     "actions": [ 
     { 
      "name": "Comment", 
      "link": "https://www.facebook.com/143873076389483/posts/143999881956342" 
     }, 
     { 
      "name": "Like", 
      "link": "https://www.facebook.com/143873076389483/posts/143999819596342" 
     } 
     ], 
     "privacy": { 
     "description": "Public", 
     "value": "EVERYONE", 
     "friends": "", 
     "networks": "", 
     "allow": "", 
     "deny": "" 
     }, 
     "type": "status", 
     "status_type": "mobile_status_update", 
     "created_time": "2014-07-14T10:58:20+0000", 
     "updated_time": "2014-07-14T10:58:20+0000" 
    }, 
    { 
     "id": "143873476389483_1439998362929721", 
     "from": { 
     "category": "Community", 
     "name": "AKA", 
     "id": "1438734076389483" 
     }, 
     "message": "Never give up", 
     "actions": [ 
     { 
      "name": "Comment", 
      "link": "https://www.facebook.com/143873406389483/posts/143999836292721" 
     }, 
     { 
      "name": "Like", 
      "link": "https://www.facebook.com/143873076389483/posts/139998362929721" 
     } 
     ], 
     "privacy": { 
     "description": "Public", 
     "value": "EVERYONE", 
     "friends": "", 
     "networks": "", 
     "allow": "", 
     "deny": "" 
     }, 
     "type": "status", 
     "status_type": "mobile_status_update", 
     "created_time": "2014-07-14T10:55:16+0000", 
     "updated_time": "2014-07-14T10:55:16+0000" 
    }, 
    { 
     "id": "1438734076389483_1439998316263059", 
     "from": { 
     "category": "Community", 
     "name": "AKA", 
     "id": "1438734076389483" 
     }, 
     "message": "lemme try", 
     "actions": [ 
     { 
      "name": "Comment", 
      "link": "https://www.facebook.com/143873476389483/posts/143998316263059" 
     }, 
     { 
      "name": "Like", 
      "link": "https://www.facebook.com/143734076389483/posts/143998316263059" 
     } 
     ], 
     "privacy": { 
     "description": "Public", 
     "value": "EVERYONE", 
     "friends": "", 
     "networks": "", 
     "allow": "", 
     "deny": "" 
     }, 
     "type": "status", 
     "status_type": "mobile_status_update", 
     "created_time": "2014-07-14T10:54:54+0000", 
     "updated_time": "2014-07-14T10:54:54+0000" 
    }, 
    { 
     "id": "143873407639483_1439990399597184", 
     "from": { 
     "category": "Community", 
     "name": "AKA", 
     "id": "143873407389483" 
     }, 
     "message": "AAAAAAAAAAAAAAA BBBBBBBBBBBBBBB", 
     "actions": [ 
     { 
      "name": "Comment", 
      "link": "https://www.facebook.com/143734076389483/posts/143990399597184" 
     }, 
     { 
      "name": "Like", 
      "link": "https://www.facebook.com/143734076389483/posts/143999039957184" 
     } 
     ], 
     "privacy": { 
     "description": "Public", 
     "value": "EVERYONE", 
     "friends": "", 
     "networks": "", 
     "allow": "", 
     "deny": "" 
     }, 
     "type": "status", 
     "status_type": "mobile_status_update", 
     "created_time": "2014-07-14T10:12:51+0000", 
     "updated_time": "2014-07-14T10:12:51+0000" 
    }, 
    { 
     "id": "143873407689483_143998952930603", 
     "from": { 
     "category": "Community", 
     "name": "AKA", 
     "id": "1438734076389483" 
     }, 
     "message": "hello", 
     "actions": [ 
     { 
      "name": "Comment", 
      "link": "https://www.facebook.com/143734076389483/posts/14399895420603" 
     }, 
     { 
      "name": "Like", 
      "link": "https://www.facebook.com/143873407638483/posts/143989542930603" 
     } 
     ], 
     "privacy": { 
     "description": "Public", 
     "value": "EVERYONE", 
     "friends": "", 
     "networks": "", 
     "allow": "", 
     "deny": "" 
     }, 
     "type": "status", 
     "status_type": "mobile_status_update", 
     "created_time": "2014-07-14T10:05:09+0000", 
     "updated_time": "2014-07-14T10:05:09+0000" 
    } 
    ], 
    "paging": { 
    "previous": "https://graph.facebook.com/v2.0/143873476389483/feed?limit=25&since=140363783", 
    "next": "https://graph.facebook.com/v2.0/143873406389483/feed?limit=25&until=140532308" 
    } 
} 
각 항목 당신이 얻을 것을 가정 한 후

for (int i = 0; i < dataJSONArray.length() ; i++) { 
    JSONObject c = dataJSONArray.getJSONObject(i); 

그러나 :

그리고 여기 당신이 data 섹션의 항목을 반복하고 예외를

      JSONObject oneRowJSONObj = response 
            .getGraphObject() 
            .getInnerJSONObject(); 

          JSONArray dataJSONArray = oneRowJSONObj 
            .getJSONArray("data"); 

          Log.v(TAG, dataJSONArray.toString()); 

//Working 

          for (int i = 0; i < dataJSONArray.length() ; i++) { 
           JSONObject c = dataJSONArray 
             .getJSONObject(i); 

           // Storing JSON item in a Variable 
           String message = c.getString("message"); 


           Log.v("ONE ROW", message);//Here i get the exception 

답변

0

그래서 ...을 던져 내 코드입니다 그 배열에서 "메시지"하나입니다 :

String message = c.getString("message"); 

하지만 불행히도 단지 그것을 포함하지 않습니다. 배열에는 ID, 보낸 사람, 메시지, 작업, 개인 정보 보호 등 data 하위 섹션의 모든 키가 있습니다.

따라서 첫 번째 반복이 단지 JSONObject이 될 가능성이 있습니다 열쇠 "id"를 가지고있다. c에 "메시지"값이 있는지 확인하지 않으므로 예외가 발생합니다. 키가 없으면 getString()으로 전화 할 수 없습니다. if (c.has("message")을 수행하거나 optString()으로 전화하여 선택적 매개 변수를 지정할 수 있습니다.

+0

감사 프로세스가 진행되었습니다. 그것은 실제로'c.has ("message")'c.hasString ("message")가 아닙니다. – Saf

+0

@ user3838247 whoops - 그것을 사용하여 내 코드를 찾을 수없고 메모리를 사용하지 못했습니다. 올바른 것입니다. –