Created
July 26, 2019 21:56
-
-
Save vacax/2053e975a14959d81d0f53ac77c6da23 to your computer and use it in GitHub Desktop.
Ejemplo de leer estudiantes de Servidor Sparkjava-Restful
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
System.out.println("Hola Mundo Rest Cliente :-D"); | |
HttpResponse<JsonNode> response = Unirest.get("http://localhost:4567/rest/estudiantes/") | |
.header("accept", "application/json") | |
.queryString("apiKey", "123") | |
.asJson(); | |
// | |
System.out.println("Codigo respuesta del HTTP: "+response.getStatus()); | |
if(response.getStatus() != 200){ | |
System.out.println("Se presentó un error en la llamada"); | |
return; | |
} | |
JsonNode json = response.getBody(); | |
System.out.println("Es un arreglo? "+json.isArray()); | |
int cantidadElementos = json.getArray().length(); | |
System.out.println("La cantidad de elementos: "+cantidadElementos); | |
for(int i=0;i<cantidadElementos;i++){ | |
//recuperando la referencia del objeto. | |
JSONObject jsonObject = json.getArray().getJSONObject(i); | |
//presentando la información. | |
System.out.println(String.format("Estudiante[%d]-> { matricula: %d, nombre: %s }", | |
i, | |
jsonObject.getInt("matricula"), | |
jsonObject.getString("nombre"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment