简体   繁体   English

libgdx的http请求方法不适用于Web应用程序

[英]libgdx's http request method doesn't work on the web application

I'm testing the net class of libgdx library to get a json from the page passed to the setUrl method. 我正在测试libgdx库的net类,以便从传递给setUrl方法的页面获取json。 This portion of code comes from a class called ApplicationScreen that implements the libgdx Screen interface. 这部分代码来自一个名为ApplicationScreen的类,它实现了libgdx Screen界面。 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. 当我运行桌面版本时,我在控制台窗口“HTTP请求状态:200”上收到此消息,然后是http请求的内容,因此代码执行了我期望它应该执行的操作。 When I run instead the html version I get this message on the Console "HTTP Request status: 0". 当我运行html版本时,我在控制台“HTTP请求状态:0”上收到此消息。 Any suggestion? 有什么建议吗?

Did you set up the permissions in the android manifest? 你在android清单中设置了权限吗?

How to add manifest permission to android application? 如何向android应用程序添加清单权限?

Also need to check that you use the ip of the computer you are using when passing the the url rather than localhost. 还需要检查您在传递url时使用的是您正在使用的计算机的ip而不是localhost。

Hope this is of some help...even if it is long overdue. 希望这有一些帮助......即使它早就应该了。

这可能为时已晚,但就我而言,事实证明这是违反同源政策的行为。

我使用不安全版本的Google Chrome解决了这个问题,使用以下选项启动浏览器: - allow-file-access-from-files --disable-web-security

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM