3

I'm testing the net class of libgdx library to get a json from the page passed to the setUrl method. This portion of code comes from a class called ApplicationScreen that implements the libgdx Screen interface. That's the code:

@Override
public void show() {

    String requestContent = null;

    httpRequest = new HttpRequest(Net.HttpMethods.GET);
    httpRequest.setUrl("http://localhost/projects/SqlParser/index.php");
    httpRequest.setHeader("Content-Type", "text/plain");
    httpRequest.setContent(requestContent);

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

        public void handleHttpResponse(HttpResponse httpResponse) {

            final int statusCode = httpResponse.getStatus().getStatusCode();

            System.out.println("HTTP Request status: " + statusCode);
            System.out.println("Content:");
            System.out.println(httpResponse.getResultAsString());


        }

        public void failed(Throwable t) {
            System.out.println("HTTP request failed!");
        }

    });

}

When I run the desktop version I get this message on the Console window "HTTP Request Status: 200", followed by the content of the http request, so the code does what I expect it should do. When I run instead the html version I get this message on the Console "HTTP Request status: 0". Any suggestion?

3 Answers3

1

Did you set up the permissions in the android manifest?

How to add manifest permission to android application?

Also need to check that you use the ip of the computer you are using when passing the the url rather than localhost.

Hope this is of some help...even if it is long overdue.

Community
  • 1
  • 1
afield
  • 283
  • 1
  • 3
  • 13
1

This may come too late but in my case it turned out to be a Same-Origin-Policy violation.

Kevin
  • 120
  • 7
0

I solved the problem using an unsecured version of Google Chrome, launching the browser with these options: --allow-file-access-from-files --disable-web-security