Async REST web services calls
Sebastian Gutierrez
scgm11 at gmail.com
Thu Feb 14 10:10:07 PST 2013
Hello list,
I'm migrating a silverlight app to javafx, but I'm running into this issue:
I used a lot async calls to REST Webservices on siverlight like this:
WebClient test = new WebClient();
test.UploadStringCompleted += new UploadStringCompletedEventHandler(test_UploadStringCompleted);
test.UploadStringAsync(URL, POST DATA);
and the callback method:
void test_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
String result = e.Response();
//here I have the response of the web service
}
now Im using this on JAVAFX:
public static String ExecPOSTRESTWebService(String resource, String data)
{
try {
String rest = //URL for the rest WS;
URL url = new URL(rest);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
BufferedReader rd;
String buffer;
try (OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream())) {
wr.write(data);
wr.flush();
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
buffer = "";
while ((line = rd.readLine()) != null) {
buffer = buffer + line;
}
}
rd.close();
return buffer;
} catch (Exception ex) {
Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, ex.toString());
return "";
}
}
the problem is that this is sync and not async so I block the UI, I know I can do some threading and Platform.runlater… and those… but for many calls the code will get hard to read and difficult
is there any component around that does the async calls with a callback method?? any library?
Thanks!
More information about the openjfx-dev
mailing list