Ejemplo n.º 1
0
  private void registerJson() {
    JsonObject json = new JsonObject();
    json.add("username", new JsonPrimitive(userField.getText()));
    json.add("password", new JsonPrimitive(passwordField.getText()));
    json.add("email", new JsonPrimitive(emailField.getText()));

    final Net.HttpRequest httpRequest = new Net.HttpRequest(Net.HttpMethods.POST);
    httpRequest.setUrl(Assets.HTTP_SERVER + "registration");
    httpRequest.setHeader("Content-Type", "application/json");
    httpRequest.setContent(json.toString());
    Gdx.net.sendHttpRequest(
        httpRequest,
        new Net.HttpResponseListener() {
          @Override
          public void handleHttpResponse(final Net.HttpResponse httpResponse) {
            System.out.print(httpResponse.getResultAsString());
            if (httpResponse.getStatus().getStatusCode() == 200) {
              Gdx.app.postRunnable(
                  new Runnable() {
                    @Override
                    public void run() {
                      stage.addAction(
                          Actions.sequence(
                              Actions.fadeOut(1),
                              Actions.run(
                                  new Runnable() {
                                    @Override
                                    public void run() {
                                      login(
                                          userField.getText().toString(),
                                          passwordField.getText().toString(),
                                          false);
                                      game.setScreen(new PlayScreen(game));
                                    }
                                  })));
                    }
                  });
            }
          }

          @Override
          public void failed(Throwable t) {
            labelMessage.setText("Please check your Internet connection.");
          }

          @Override
          public void cancelled() {
            Gdx.app.postRunnable(
                new Runnable() {
                  @Override
                  public void run() {
                    //                     ?????
                  }
                });
          }
        });
  }
Ejemplo n.º 2
0
  /**
   * Make a new Graph Request. View:
   * https://developers.facebook.com/docs/graph-api/reference/v2.4/request
   *
   * @param request
   * @param callback
   */
  public void newGraphRequest(
      GDXFacebookGraphRequest request, final GDXFacebookCallback<JsonResult> callback) {
    String accessToken = null;
    if (getAccessToken() != null) {
      accessToken = getAccessToken().getToken();
    }

    if (request.isUseCurrentAccessToken() && accessToken != null) {
      request.putField("access_token", accessToken);
    }

    Net.HttpRequest httpRequest = new Net.HttpRequest(request.getMethod());
    String url = request.getUrl() + config.GRAPH_API_VERSION + "/" + request.getNode();
    httpRequest.setUrl(url);
    httpRequest.setContent(request.getContentAsString());
    httpRequest.setTimeOut(request.getTimeout());

    HttpRequestBuilder builder = new HttpRequestBuilder().newRequest();
    builder.method(request.getMethod());

    Gdx.net.sendHttpRequest(
        httpRequest,
        new Net.HttpResponseListener() {

          @Override
          public void handleHttpResponse(Net.HttpResponse httpResponse) {
            String resultString = httpResponse.getResultAsString();
            int statusCode = httpResponse.getStatus().getStatusCode();

            if (statusCode == -1) {
              GDXFacebookError error =
                  new GDXFacebookError(
                      "Connection time out. Consider increasing timeout value by using setTimeout()");
              callback.onError(error);
            } else if (statusCode >= 200 && statusCode < 300) {
              callback.onSuccess(new JsonResult(resultString));
            } else {
              GDXFacebookError error = new GDXFacebookError("Error: " + resultString);
              callback.onError(error);
            }
          }

          @Override
          public void failed(Throwable t) {
            t.printStackTrace();
            callback.onFail(t);
          }

          @Override
          public void cancelled() {
            callback.onCancel();
          }
        });
  }