Created
April 19, 2020 13:10
-
-
Save mehrab-haque/aa2a6dd490d6e166ed14c9c2c6e78fc5 to your computer and use it in GitHub Desktop.
JavaFX async natured HTTP GET Request
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
| import javafx.animation.AnimationTimer; | |
| import org.json.*; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.Observable; | |
| import java.util.Observer; | |
| /* | |
| Author : Md. Mehrab Haque | |
| */ | |
| public class AsyncGet extends Observable { | |
| private JSONObject response; | |
| private URL url; | |
| private boolean isError; | |
| private String errorMessage; | |
| private AnimationTimer animationTimer; | |
| public AsyncGet(String string) throws MalformedURLException { | |
| url=new URL(string); | |
| isError=false; | |
| errorMessage=""; | |
| response=new JSONObject(); | |
| animationTimer=new AnimationTimer() { | |
| @Override | |
| public void handle(long l) { | |
| if(response.length()>0 || isError){ | |
| setChanged(); | |
| notifyObservers(); | |
| animationTimer.stop(); | |
| } | |
| } | |
| }; | |
| } | |
| public void send(){ | |
| animationTimer.start(); | |
| Thread thread = new Thread() { | |
| public void run() { | |
| try { | |
| HttpURLConnection con=(HttpURLConnection) url.openConnection(); | |
| int res=con.getResponseCode(); | |
| String data=""; | |
| if(res==200) { | |
| BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
| String input; | |
| while ((input = br.readLine()) != null) { | |
| data+=input; | |
| } | |
| response=new JSONObject(data); | |
| } | |
| } catch(Exception v) { | |
| isError=true; | |
| errorMessage=v.getMessage(); | |
| } | |
| } | |
| }; | |
| thread.start(); | |
| } | |
| public void reset(){ | |
| isError=false; | |
| errorMessage=""; | |
| response=new JSONObject(); | |
| } | |
| public JSONObject getResponse() { | |
| return response; | |
| } | |
| public boolean isError() { | |
| return isError; | |
| } | |
| public String getErrorMessage() { | |
| return errorMessage; | |
| } | |
| } |
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
| AsyncGet getRequest=new AsyncGet("URL_OF_GET_API"); | |
| Observer observer=new Observer() { | |
| @Override | |
| public void update(Observable o, Object arg) { | |
| if(getRequest.isError()){ | |
| //There is an error | |
| System.out.println(getRequest.getErrorMessage()); | |
| }else { | |
| //Response is received | |
| //Change JavaFX UI | |
| System.out.println(getRequest.getResponse()); | |
| } | |
| getRequest.reset(); | |
| } | |
| }; | |
| getRequest.addObserver(observer); | |
| getRequest.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment