title: Synchronous responses to `Gdx.net.sendHttpRequest` in LibGDX
body: <p>I'm making a small game in LibGDX. I'm saving the player's username locally as well as on a server. The problem is that the application is not waiting for the result of the call so the online database's ID is not saved locally. Here's the overall flow of the code:</p>
  
  <pre><code>//Create a new user object
  User user = new User(name);
  
  //Store the user in the online database
  NetworkService networkService = new NetworkService();
  String id = networkService.saveUser(user);
  
  //Set the newly generated dbase ID on the local object
  user.setId(id);
  
  //Store the user locally
  game.getUserService().persist(user);
  </code></pre>
  
  <p>in this code, the <code>id</code> variable is not getting set because the <code>saveUser</code> function is returning immediately. How can I make the application wait for the result of the network request so I can work with results from the server communication?</p>
  
  <p>This is the code for <code>saveUser</code>:</p>
  
  <pre><code>public String saveUser(User user) {
      Map&lt;String, String&gt; parameters = new HashMap&lt;String, String&gt;();
      parameters.put("action", "save_user");
      parameters.put("json", user.toJSON());
  
      HttpRequest httpGet = new HttpRequest(HttpMethods.POST);
      httpGet.setUrl("http://localhost:8080/provisioner");
      httpGet.setContent(HttpParametersUtils.convertHttpParameters(parameters));
  
      WerewolfsResponseListener responseListener = new WerewolfsResponseListener();
      Gdx.net.sendHttpRequest (httpGet, responseListener);
      return responseListener.getLastResponse();
  }
  </code></pre>
  
  <p>This is the <code>WerewolfsResponseListener</code> class:</p>
  
  <pre><code>class WerewolfsResponseListener implements HttpResponseListener {
      private String lastResponse = "";
      public void handleHttpResponse(HttpResponse httpResponse) {
          System.out.println(httpResponse.getResultAsString());
          this.lastResponse = httpResponse.getResultAsString();    
      }
  
      public void failed(Throwable t) {
         System.out.println("Saving user failed: "+t.getMessage());
         this.lastResponse = null;
      }
  
      public String getLastResponse() {
          return lastResponse;
      }
  }
  </code></pre>