Skip to content

Instantly share code, notes, and snippets.

@Shujito Shujito/http.java
Last active Aug 29, 2015

Embed
What would you like to do?
ejemplo HttpURLConnection, le faltan try catches
// nueva conexion
URL url = new URL("http://danbooru.donmai.us/posts.json?tags=setz");
// abrir
HttpURLConnection https = (HttpURLConnection) url.openConnection();
// conectar
https.connect();
// 200,401,404,500
int code = https.getResponseCode();
// OK, Access denied, Not found, Internal server error
String message = https.getResponseMessage();
// abrir el inputstream en un buffer
BufferedReader br = new BufferedReader(new InputStreamReader(https.getInputStream()));
// leer linea por linea
String l = null;
StringBuilder sb = new StringBuilder();
while ((l = br.readLine()) != null)
sb.append(l);
// convertir stringbuilder a stirng
String jsonString = sb.toString();
// parsear el string como jsonarr o jsonobject, dependiendo
JSONArray jarr = new JSONArray(jsonString);
// etc
JSONObject jobj = jarr.getJSONObject(0);
int id = jobj.getInt("id");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.