Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I thread with sockets so that the program doesn't stop
    text
    copied!<p>It seems that when I use the accept method from the Socket class the whole program freezes up until data comes through. I've passed the socket to a thread and it doesn't freeze but I need the thread to return data back which I don't think it can do.</p> <p><strong>Code For getting useername</strong></p> <pre><code>public boolean checkUsername() { NetworkIO n = new NetworkIO(); // Grabs username from edittext field username = usernameEditText.getText().toString(); System.out.println(usernameEditText.getText().toString()); // queries databse for username try { resultFromServer = n.query("username", "select username FROM user_info WHERE MATCH (username) AGAINST ('" + username + "' IN NATURAL LANGUAGE MODE);"); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <p><strong>Code for NetworkIO class</strong></p> <pre><code>public class NetworkIO extends Activity { Socket networkSocket = null; PrintWriter out = null; BufferedReader in = null; public String query(String request, String fieldRequested) throws IOException { // Tries to get connection try { networkSocket = new Socket("192.168.1.8", 9009); out = new PrintWriter(networkSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( networkSocket.getInputStream())); System.out.println("Hi from in made"); } catch (UnknownHostException e) { System.err.println("Unknown host"); System.exit(-1); } catch (IOException e) { System.err.println("IOException"); System.exit(-1); } //Sends Request out.println(request); out.println(fieldRequested); String resultFromServer = ""; //Waits for response and if its null exit while ((in.readLine()) != null) { resultFromServer += in.readLine(); } System.out.println(resultFromServer); //Close the connections out.close(); in.close(); networkSocket.close(); return resultFromServer; } } </code></pre> <p>Any criticism of my code will be much appreciated :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload