HTTP POST highscore JSON object with LibGDX

Hey guys!

I’d like to post highscores from my game through HTTP. I understand this can be done easily with LibGDX’s Net-module, if I provide an InputStream with the data I want transferred.

I’m not very good working with streams over the network, so I don’t know the pitfalls and what I need to be wary about.

How would I go about sending a JSON object (from memory), to my webserver, using POST, and LibGDX?

Thanks guys. :slight_smile:

EDIT:

HttpRequest httpPost = new HttpRequest(HttpMethods.Post);
httpPost.setUrl("http://highscore-server.net/highscore-accepting-script.php");
httpPost.setContent(stream, length);

Gdx.net.sendHttpRequest (httpPost, new HttpResponseListener() {
   public void handleHttpResponse(HttpResponse httpResponse) {
      status = httpResponse.getResultAsString();
   }

   public void failed(Throwable t) {
      status = "failed";
   }
 });

This as far as I can get, code-wise. I don’t know what to do for the InputStream.

I’d probably do something like this (no need to make a stream of your string based content):


HttpRequest httpPost = new HttpRequest(HttpMethods.Post);
httpPost.setUrl("http://highscore-server.net/highscore-accepting-script.php");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setContent(yourJsonString);

Gdx.net.sendHttpRequest (httpPost, new HttpResponseListener() {
   public void handleHttpResponse(HttpResponse httpResponse) {
      status = httpResponse.getResultAsString();
   }

   public void failed(Throwable t) {
      status = "failed";
   }
 });

That seems very much like what I’d need. Thanks so much. :slight_smile: