Created
March 21, 2016 20:22
-
-
Save subh007/5c9d2e1de06b53cdd106 to your computer and use it in GitHub Desktop.
Async_Architecture
This file contains hidden or 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
interface Fetcher { | |
public void downloadURL(String url, FetcherCallback callbackHandler); | |
} | |
interface FetcherCallback { | |
public void onData(String data); | |
public void onError(Exception e); | |
} | |
public class AsyncRequest implements FetcherCallback{ | |
public void makeRequest(String url) { | |
new Fetcher() { | |
@Override | |
public void downloadURL(String url, FetcherCallback callbackHandler) { | |
callbackHandler.onData("we got some data."); | |
try { | |
URL downloadURL = new URL(url); | |
URLConnection yc = downloadURL.openConnection(); | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader( | |
yc.getInputStream())); | |
String inputLine; | |
while ((inputLine = in.readLine()) != null) | |
callbackHandler.onData(inputLine); | |
in.close(); | |
} catch (Exception e) { | |
callbackHandler.onError(e); | |
} | |
} | |
}.downloadURL(url, this); | |
} | |
@Override | |
public void onData(String data) { | |
// TODO Auto-generated method stub | |
System.out.println(data); | |
} | |
@Override | |
public void onError(Exception e) { | |
// TODO Auto-generated method stub | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment