Skip to content

Instantly share code, notes, and snippets.

@osdrv
Created January 4, 2012 22:00
Show Gist options
  • Save osdrv/1562411 to your computer and use it in GitHub Desktop.
Save osdrv/1562411 to your computer and use it in GitHub Desktop.
simple Java http loader with labmda-handlers
import java.net.*;
import java.io.*;
interface LambdaHandler {
public void success( String response );
}
class XMLLoader implements Runnable {
LambdaHandler handler;
URL path;
Thread runner;
void load( String path, LambdaHandler handler ) throws Exception {
this.handler = handler;
this.path = new URL( path );
runner = new Thread( this );
runner.start();
}
public void run() {
try {
URLConnection connection = path.openConnection();
BufferedReader input = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
String inputLine;
StringBuffer res = new StringBuffer();
while ( ( inputLine = input.readLine() ) != null ) {
res.append( inputLine + "\n" );
}
input.close();
handler.success( res.toString() );
} catch ( Exception e ) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment