Skip to content

Instantly share code, notes, and snippets.

@hminaya
Last active May 11, 2018 10:45
Show Gist options
  • Save hminaya/5384625 to your computer and use it in GitHub Desktop.
Save hminaya/5384625 to your computer and use it in GitHub Desktop.
get JSON api with Java
package com.hminaya.storage;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
public class prueba {
public static void main(String []args){
//initialize
InputStream is = null;
String result = "";
JSONObject jArray = null;
String url = "http://www.emplea.do/jobs.json";
//http post
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
//convert response to string
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
//try parse the string to a JSON object
jArray = new JSONObject(result);
System.out.println(jArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment