2017-07-05 44 views
1

如何處理對象的Android 這種反應不處理如何處理的Android的Facebook評論API響應

如何處理的Android的Facebook評論API響應

{ 
     "data": [ 
     { 
      "created_time": "2017-06-26T04:54:27+0000", 
      "from": { 
      "name": "Manish", 
      "id": "1120172725955" 
      }, 
      "message": "Working", 
      "id": "751199848395776_760186854163742" 
     }, 
     { 
      "created_time": "2017-06-26T05:00:17+0000", 
      "from": { 
      "name": "Kumar Vishwas", 
      "id": "744511125731315" 
      }, 
      "message": "hi", 
      "id": "751199848395776_760188187496942" 
     } 
     ], 
     "paging": { 
     "cursors": { 
      "before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOell3TVRnMk9EVTBNVFl6TnpReU9qRTBPVGcwTlRJNE5qYz0ZD", 
      "after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOell3TVRnNE1UZAzNORGsyT1RReU9qRTBPVGcwTlRNeU1UYz0ZD" 
     } 
     } 
    } 

回答

1
ArrayList<CommentsPojo> commentsPojos =new ArrayList<>(); 

public void handleResponse() { 
     try { 
      JSONObject jsonData = new JSONObject(response); // Getting Array of Contacts 
      JSONArray jsonArray=jsonData.getJSONArray("data"); 

      Log.e("MainActivity ", "jsondata --> " + jsonArray.toString(2)); 
      Log.e("MainActivity ", "jsonArray.length()-> " + jsonArray.length()); 

      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsondata = jsonArray.getJSONObject(i).getJSONObject("from"); 
       JSONObject json=jsonArray.getJSONObject(i); 

       Log.e("MainActivity ", "message --> " + json.getString("message")); 
       Log.e("MainActivity ", "id --> " + jsondata.getString("id")); 
       Log.e("MainActivity ", "name --> " + jsondata.getString("name")); 

       CommentsPojo commentsPojo = new CommentsPojo(); 
       commentsPojo.setId(jsondata.getString("id")); 
       commentsPojo.setName(jsondata.getString("name")); 
       commentsPojos.add(commentsPojo); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

日誌中的響應請參閱

E/MainActivity: message --> Working 
E/MainActivity: id --> 1120172725955 
E/MainActivity: name --> Manish 
E/MainActivity: message --> hi 
E/MainActivity: id --> 744511125731315 
E/MainActivity: name --> Kumar Vishwas