Created
January 4, 2012 22:00
-
-
Save osdrv/1562411 to your computer and use it in GitHub Desktop.
simple Java http loader with labmda-handlers
This file contains 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
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