Example #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() {
                    //                     ?????
                  }
                });
          }
        });
  }
  public String httpGet(String URL, final int ListNumber) {

    httpReturns.add(ListNumber, "");

    Net.HttpRequest httpRequest = new Net.HttpRequest(Net.HttpMethods.GET);
    httpRequest.setUrl(URL);
    httpRequest.setHeader("Content-Type", "application/json");

    Gdx.net.sendHttpRequest(
        httpRequest,
        new Net.HttpResponseListener() {
          @Override
          public void handleHttpResponse(Net.HttpResponse httpResponse) {
            System.out.println("handle");
            String successValue = httpResponse.getResultAsString();
            System.out.println(successValue);
            if (successValue.contains("\"total_count\": 0")) // wrong credentials
            {
              httpReturns.set(ListNumber, "EMPTY");
              // menuComponents.add(3, new MenuComponent(300, 400, 100, 50,
              // Assets.menuFailedRegion));
            } else // there was a match yo! should probably have a unique conststraint on username.
                   // too hard eff it
            {
              httpReturns.set(ListNumber, successValue);
              /*JSONObject json = new JSONObject(successValue);
              String username = json.getString("username");
              int wins = json.getInt("wins");
              int losses = json.getInt("losses");
              String charityURL = json.getString("charity");*/

            }
          }

          @Override
          public void failed(Throwable t) {
            System.out.println("failed");
            httpReturns.set(ListNumber, "FAILED");
            System.out.println(t.toString());
          }

          @Override
          public void cancelled() {

            System.out.println("cancelled");
            httpReturns.set(ListNumber, "CANCELLED");
          }
        });

    while (httpReturns.get(ListNumber).length()
        < 1) // while its empty because the HTTP method hasnt returned yet
    {}

    return httpReturns.get(ListNumber);
  }
Example #3
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();
          }
        });
  }