LibGDX HTTP请求不起作用 [英] LibGDX http request not working

查看:71
本文介绍了LibGDX HTTP请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在我为Android设计的LibGDX游戏中按下特定按钮时,我想向数据库发送字符串消息.我该怎么做呢?以下是我尝试过的代码.但这是行不通的.

I want to send a String message to database when user presses a specific button in the LibGDX game I am designing for android. How do I go about doing that? Following is the code I tried. But it does not work.

 Net.HttpRequest httpRequest = new Net.HttpRequest();
    httpRequest.setMethod("POST");
    httpRequest.setUrl("URL is here");
    httpRequest.setContent("INSERT INTO `game_table` (`Button`) VALUES ('Button 1 Pressed')");

    Net.HttpResponseListener httpResponseListener = new Net.HttpResponseListener() {
        @Override
        public void handleHttpResponse(Net.HttpResponse httpResponse) {
            Gdx.app.log("Log httpResponse", httpResponse.getResultAsString());
        }

        @Override
        public void failed(Throwable t) {

        }

        @Override
        public void cancelled() {

        }
    };
    Gdx.net.sendHttpRequest(httpRequest,httpResponseListener);

日志在android监视器中未提供任何内容.我也尝试使用AsyncTask而不使用AsyncTask来实现此代码.但是两者都不起作用.我想念什么吗?如果是这样,你能给我一个可行的小代码片段吗?

Log does not provide anything in android monitor. I also tried using AsyncTask and without AsyncTask to implement this code. But neither works. Am I missing something? If so could you give me small code snippet that will work?

推荐答案

您不需要使用AsyncTask,libGDX的HTTPRequest是开箱即用的.

You don't need to use an AsyncTask, libGDX' HTTPRequest is async out of the box.

如果请求失败或被取消,则您未记录任何内容.

You did not log anything if the request fails or is cancelled so probably that's the case.

这篇关于LibGDX HTTP请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆