@SuppressWarnings("unchecked") protected <T> Connection<T> mapToConnection(String connectionJson, Class<T> connectionType) { List<T> data = new ArrayList<T>(); String previous = null; String next = null; try { JsonObject jsonObject = new JsonObject(connectionJson); // Pull out data JsonArray jsonData = jsonObject.getJsonArray("data"); for (int i = 0; i < jsonData.length(); i++) data.add(connectionType.equals(JsonObject.class) ? (T) jsonData.get(i) : jsonMapper.toJavaObject(jsonData .get(i).toString(), connectionType)); // Pull out paging info, if present if (jsonObject.has("paging")) { JsonObject jsonPaging = jsonObject.getJsonObject("paging"); previous = jsonPaging.has("previous") ? jsonPaging.getString("previous") : null; next = jsonPaging.has("next") ? jsonPaging.getString("next") : null; } } catch (JsonException e) { throw new FacebookJsonMappingException("Unable to map connection JSON to Java objects", e); } return new Connection<T>(data, previous, next); }
/** * @see com.restfb.FacebookClient#fetchConnection(java.lang.String, java.lang.Class, com.restfb.Parameter[]) */ @Override public <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters) { verifyParameterPresence("connection", connection); verifyParameterPresence("connectionType", connectionType); Connection<T> conn = new Connection<>(this, makeRequest(connection, parameters), connectionType); if (conn.getNextPageUrl() == null && conn.getAfterCursor() != null) { String fullUrl = createEndpointForApiCall(connection, false); String paramString = toParameterString(parameters); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl + "?" + paramString, "after", conn.getAfterCursor()); conn.setNextPageUrl(fullUrl); } if (conn.getPreviousPageUrl() == null && conn.getBeforeCursor() != null) { String fullUrl = createEndpointForApiCall(connection, false); String paramString = toParameterString(parameters); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl + "?" + paramString, "before", conn.getBeforeCursor()); conn.setPreviousPageUrl(fullUrl); } return conn; }
Connection<T> conn = new Connection<>(this, connectionJson, connectionType);