• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create a task in a JavaFX application (with a runnable) that handles data from a web server?  RSS feed

 
Josef Lundström
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am developing a game of Quiz, with a web server and a client. Now, when the client connects to the server, it crashes. The reason is that code which takes a long time to run, should be executed on a background thread and not on the FX Application Thread. This means that I have to:

Create a task, that handles the data from the web server.
Register a handler that sends the results to the UI when it's complete.
Invoke the task on a background thread.

But I how do I do this when it's a runnable method that is handling the input from the server? I am trying to implement the tutorial that you can find at  Using threads to make database request Using threads to make database requests

The problems occur when I am coming to this part of the code:

}

The Client Class:


 
John Damien Smith
Ranch Hand
Posts: 354
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are so many things that are wrong with this code that is hard to know where to even begin.

You register an action handler within an action handler.
You use some obscure sun snmp management API for runnable thread state.
You use raw sockets to talk to a web server rather than http.
You ask the user to input a server IP address (domain name is better and the address should be in a configuration file rather than user input).
You don't even use the IP address entered, but instead just use a hardcoded localhost.
You don't follow standard camel case naming conventions for variables.
Your task returns a runnable rather the data being fetched.
Your task call method doesn't actually do anything.
You have @FXML injected fields which you set to new fields.
You have fields such as IP and bQuit which don't seem to be declared so the code won't compile.

I don't have time to provide you with a solution now.

You can review:
http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests
To understand at least the http retrieval part.

I recommend:
a. Get your quiz game to work without any threading or networking.
b. Then convert it to add the networking part with no threading (http is usually pretty quick with small data as you have unless the server is down or something, so it won't cause long pauses in user interaction).
c. Finally get it working with threading only after all of the other stuff is working *perfectly*.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!