I am new to Networking and Databases, but im trying to learn MySQL and HTTP Requests to create a user Login for a game.

My goal is to let users create accounts and login to them etc (for Highscores etc). I discovered the Libgdx Net Module and also got my server running with MySQL, Apache and PHP. I want to use php files to communicate with the Database on the Server.

But my question is (since there is not a big documention for libgdx Network Module): Where and how can i learn how to create proper HTTP Requests in LibGDX and should i use JSON? Just give me a hint how to start.

Thanks ;)

  • \$\begingroup\$ Why do you want to do this ? There is no API in java for MySQL commands ? or there is and it is not crossplatform supported by the libgdx libraries ? \$\endgroup\$ – dimitris93 Sep 16 '15 at 7:19
  • \$\begingroup\$ @shiro That is what he needs webrequests for. Sent a PHP script to work with the database and receive dynamic data from it. The way I do it is having a Java server in between client and database, that is a lot more work to setup though. \$\endgroup\$ – Madmenyo Sep 16 '15 at 11:56
  • \$\begingroup\$ @MennoGouw I am a bit surprised because I have had used a library in C# that could make MySQL connections to a database and could query the database, without any code (like php) on the server side at all. \$\endgroup\$ – dimitris93 Sep 16 '15 at 12:23
  • \$\begingroup\$ @shiro jdbc is a java library to do exactly the same. I'm not sure however if it's compatible with platforms other then desktop. I'm using that on the server side of my wip game. \$\endgroup\$ – Madmenyo Sep 16 '15 at 12:53
  • \$\begingroup\$ I am using php in-between because its lot more secure than directly connecting to the database. \$\endgroup\$ – user72229 Sep 16 '15 at 16:32

This is somewhat what I am doing now. I use the KryoNet library also created by Nathan of LibGDX. I have a KryoNet server that my LibGDX app connects to and that server uses the jdbc to connect to a MySQL server locally. So actually without webrequests since my server is local to the DB.

Having a server between the database and your clients is not really necessary though. But I want multiplayer game mechanics that need to be handled on the server. And a TCP connection is a lot faster then webrequests.

Now for your actual question, if you want to use webrequests to run a php script for working with your database then look at the following code.

builder = new HttpRequestBuilder();
        request = builder.newRequest().method(Net.HttpMethods.GET).url("http://yourdomain.com/script.php").build();
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");

final long start = System.nanoTime(); //for checking the time until response
        Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() {
            public void handleHttpResponse(Net.HttpResponse httpResponse) {
                Gdx.app.log("WebRequest", "HTTP Response code: " + httpResponse.getStatus().getStatusCode());
                Gdx.app.log("WebRequest", "HTTP Response code: " + httpResponse.getResultAsString());
                Gdx.app.log("WebRequest", "Response time: " + ((System.nanoTime() - start) / 1000000) + "ms");

            public void failed(Throwable t) {
                Gdx.app.log("WebRequest", "HTTP request failed");

            public void cancelled() {
                Gdx.app.log("WebRequest", "HTTP request cancelled");

You can also send back XML and HTML if you supply the content-type in the header. Then just use your favorite parser to parse the data in your app. You can just grab it within the listeners httpResponse.getResultAsString() or httpResponse.getResultAsStream() if you like to work with a stream.

This is some php I wrote to receive XML when I wanted the same as you, then I switched over to the client -> server -> database architecture.

$link = mysqli_connect("", "root", "", "test");
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><mysql></mysql>");
if (!$link)
    $xml->addChild("error", "Error: Unable to connect to MySQL.");
    $xml->addChild("error", "Debugging errno: " . mysqli_connect_errno());
    $xml->addChild("error", "Debugging error: " . mysqli_connect_error());

$xml->addChild("success", "Success: A proper connection to MySQL was made!");
$xml->addChild("success", "Host information: " . mysqli_get_host_info($link));

//To send the xml data back:


Take note that you do have to change the header to receive xml data in your app.

| improve this answer | |
  • \$\begingroup\$ Thanks for your detailed answer. Using Kryonet sounds very interesting to me, because i am already using Kryonet to provide Multiplayer. I am currently running Kryonet on the Android client (which connects to..) and on my Debian Server (as a Jar), which embodies the Game-Server. By using Kryonet i could just build this directly into my main Server Project. Would you mind to explain this a bit more in detail how you are using this strategy? \$\endgroup\$ – user72229 Sep 16 '15 at 16:40
  • \$\begingroup\$ @Sand When I need something from the client I sent a request to the server and start listening for the required "packet". When the server receives the request it uses jdbc to make a local database connection. It creates a object from the collected data and sends that over to the client who requested it. \$\endgroup\$ – Madmenyo Sep 16 '15 at 19:19
  • \$\begingroup\$ I understand this and i already implemented it ;) I just wanted to know if i need to take note of something special. But thank you. \$\endgroup\$ – user72229 Sep 16 '15 at 19:48