Skip to content

Instantly share code, notes, and snippets.

@sandsfish
Created March 23, 2013 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandsfish/5229621 to your computer and use it in GitHub Desktop.
Save sandsfish/5229621 to your computer and use it in GitHub Desktop.
Minimal setup to use Apache's HTTPClient in a Processing sketch.
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html - Apache HttpClient
// https://forum.processing.org/topic/http-post-processing - Integration with Processing (different from above)
// https://code.google.com/p/processing/source/browse/trunk/processing/java/libraries/net/examples/HTTPClient/HTTPClient.pde?r=7950 - Messy.
// https://github.com/francisli/processing-http - Unnecessary?
// This code assumes the presence of the following Apache HTTPClient JARs added to the sketch's 'code' directory:
// - httpclient-4.2.3.jar
// - httpcore-4.2.2.jar
// - commons-logging-1.1.1.jar
// This can be accomplished by manually copying, or by using 'Add File' in the Processing IDE.
// It does _not_ work to add these JARs to the sketchbook/libraries directory for some unknown reason.
String data;
PFont font;
HttpClient client = new DefaultHttpClient();
String url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=person&QueryString=barack%20obama";
HttpGet method = new HttpGet(url);
HttpResponse response = null;
HttpEntity entity = null;
void setup() {
size(800,600);
background(50);
font = createFont("Helvetica", 10, true);
textFont(font);
println("executing request: " + method.getRequestLine());
try {
response = client.execute(method);
entity = response.getEntity();
// OR: entity.writeTo(System.out);
// OR: InputStream instream = entity.getContent();
if(null != entity)
text(EntityUtils.toString(entity), 5, 15);
} catch(Exception e) {
} finally {
client.getConnectionManager().shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment