随笔 - 51,  文章 - 0,  评论 - 6,  阅读 - 32766
复制代码
package com.badlogic.gdx.tests.lwjgl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.HttpMethods;
import com.badlogic.gdx.Net.HttpRequest;
import com.badlogic.gdx.Net.HttpResponse;
import com.badlogic.gdx.Net.HttpResponseListener;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.esotericsoftware.tablelayout.Value;

public class Snippet {
  
  public static void main (String[] args) {
      
        new LwjglApplication(new ApplicationAdapter() {
            Stage stage;
            Skin skin;
            
            TextButton button;
            
            @Override
            public void create () {
                stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
                skin = new Skin(Gdx.files.internal("data/uiskin.json"));
                Gdx.input.setInputProcessor(stage);
                
                Table container = new Table();
                container.setFillParent(true);
                button = new TextButton("Click to download", skin);
                button.getStyle().disabled = skin.newDrawable("default-round", Color.DARK_GRAY);
                container.row();
                container.add(button).width(Value.percentWidth(0.50f));
                
                stage.addActor(container);
                
                button.addListener(new ClickListener() {
                    @Override
                    public void clicked (InputEvent event, float x, float y) {
                        if (!button.isDisabled()) {
                            button.setDisabled(true);
                            
                            // Make a GET request
                            HttpRequest request = new HttpRequest(HttpMethods.GET);
                            request.setTimeOut(2500);
                            request.setUrl("http://libgdx.badlogicgames.com/nightlies/libgdx-nightly-latest.zip");
                            
                            // Send the request, listen for the response                        
                            Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
                                @Override
                                public void handleHttpResponse (HttpResponse httpResponse) {
                                    // Determine how much we have to download
                                    long length = Long.parseLong(httpResponse.getHeader("Content-Length"));
    
                                    // We're going to download the file to external storage, create the streams
                                    InputStream is = httpResponse.getResultAsStream();
                                    OutputStream os = Gdx.files.external("libgdx-nightly-latest.zip").write(false);
                                    
                                    byte[] bytes = new byte[1024];
                                    int count = -1;
                                    long read = 0;
                                    try {
                                        // Keep reading bytes and storing them until there are no more.
                                        while ((count = is.read(bytes, 0, bytes.length)) != -1) {
                                            os.write(bytes, 0, count);
                                            read += count;
                                            
                                            // Update the UI with the download progress
                                            final int progress = ((int) (((double) read / (double) length) * 100));
                                            final String progressString = progress == 100 ? "Click to download" : progress + "%";
                                            
                                            // Since we are downloading on a background thread, post a runnable to touch ui                                            
                                            Gdx.app.postRunnable(new Runnable() {
                                                @Override
                                                public void run () {
                                                    if (progress == 100) {
                                                        button.setDisabled(false);
                                                    }                                                    
                                                    button.setText(progressString);
                                                }
                                            });
                                        }
                                    } catch (IOException e) {
                                        
                                    }
                                }
                                @Override
                                public void failed (Throwable t) {
                                    Gdx.app.postRunnable(new Runnable() {
                                        @Override
                                        public void run () {
                                            button.setText("Too bad. Download failed.");
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            }
            
            @Override
            public void render () {
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
                stage.act();
                stage.draw();
            }
            
        }, "Download Example", 480, 320, true);
    }
  
}
复制代码

原文 https://gist.github.com/MobiDevelop/6215682     

an example of downloading a file and displaying progress with the LibGDX Net module.

posted on   黑狱  阅读(88)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示