java - HTTP 请求后在单独的线程上更改渲染屏幕

标签 java libgdx

我的目标是当游戏无法再到达后端时更改用户的屏幕。我的代码按预期执行,只是屏幕永远不会改变。这是最初的调用:

timer.testTimeToServer(api, game);

这是计时器对象的类。我用(我的 url)代替我后端的实际 IP 地址:

public class CustomTimer {
private static final float timeToDrop = 2000;
private float time = 0;
private StopWatch watch = new StopWatch();

public void testTimeToServer(ApiCall api,final proofOfConcept game){
    watch.start();
    api.httpGetWithCallback("(my url)/api/v1/character", new CallBack(){
        @Override
        public void callback(String resp){
            System.out.println("Server Responded");
            time = watch.getTime();
            watch.stop();
            watch.reset();
            if(time > timeToDrop){
                game.setScreen(new GameOverScreen(game, false));
                System.out.println("Should have switched screen")
            }
        }
    });
   }
}

这是 api 对象中的 httpGetWithCallback 方法:

public void httpGetWithCallback (final String URL, final CallBack callback){
    Thread th = new Thread(new Runnable(){
                    @Override
                    public void run() {
                                Gdx.app.postRunnable(new Runnable() {

                                         @Override
                                         public void run() {

                                             Net.HttpRequest httpRequest = new Net.HttpRequest(Net.HttpMethods.GET);
                                             httpRequest.setUrl(URL);
                                             httpRequest.setHeader("Content-Type", "application/json");
                                             httpRequest.setTimeOut(timeoutTimeInMilli);
                                             Gdx.net.sendHttpRequest(httpRequest, new Net.HttpResponseListener() {
                                                 @Override
                                                 public void handleHttpResponse(Net.HttpResponse httpResponse) {
                                                     String successValue = httpResponse.getResultAsString();
                                                     if (successValue.contains("\"total_count\": 0"))//wrong credentials
                                                     {
                                                         callback.callback("EMPTY");
                                                     } else//there was a match yo! should probably have a unique conststraint on username. too hard eff it
                                                     {
                                                         callback.callback(successValue);
                                                     }
                                                 }

                                                 @Override
                                                 public void failed(Throwable t) {
                                                     callback.callback("FAILED");
                                                 }

                                                 @Override
                                                 public void cancelled() {
                                                     callback.callback("CANCELLED");
                                                 }
                                             });
                                         }
                                     }
                );
            }
    });

    th.start();
    threads.add(th);
}

我很困惑,因为代码打印出“应该切换屏幕”,所以它的行为就像预期的那样,除了游戏被卡住并且屏幕切换从未真正发生之外。

最佳答案

懒惰方式:

在你的主游戏类上:

public static ProofOfConcept game;

和你的方法

public void testTimeToServer(ApiCall api){
    watch.start();
    api.httpGetWithCallback("(my url)/api/v1/character", new CallBack(){
        @Override
        public void callback(String resp){
            System.out.println("Server Responded");
            time = watch.getTime();
            watch.stop();
            watch.reset();
            if(time > timeToDrop){
                Main.game.setScreen(new GameOverScreen(false));
                System.out.println("Should have switched screen")
            }
        }
    });
   }
}

正确的方法

您可以在 ProofOfConcept 类中创建一个回调,渲染方法上的每一帧都会检查结果并更改屏幕。

关于java - HTTP 请求后在单独的线程上更改渲染屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35326785/

相关文章:

java - 构建 gdata Java 程序

java - 最短路径和 Dijkstra 算法

java - 如何检测和删除 URL 中的一句话?

android - 我如何更改我的安卓游戏的图片/徽章? - libGDX

java - 是否可以将 Lombok 的构建器公开?

java - 我的SpringMVC项目因为以下错误没有运行

android - 致命信号 11 (SIGSEGV),代码 2,tid 19935 (GLThread 3723) 中的故障地址 0x9a44a2e8

java - ArcBall 相机安卓

java - Libgdx - 带延迟的随机颜色变化

java - LibGDX 中的碰撞