Search in sources :

Example 1 with HttpRequest

use of com.badlogic.gdx.Net.HttpRequest in project libgdx by libgdx.

the class DownloadTest method create.

@Override
public void create() {
    batch = new SpriteBatch();
    HttpRequest request = new HttpRequest(HttpMethods.GET);
    request.setUrl("https://www.google.at/images/srpr/logo11w.png");
    Gdx.net.sendHttpRequest(request, new HttpResponseListener() {

        @Override
        public void handleHttpResponse(HttpResponse httpResponse) {
            final byte[] bytes = httpResponse.getResult();
            Gdx.app.postRunnable(new Runnable() {

                @Override
                public void run() {
                    Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
                    texture = new Texture(new PixmapTextureData(pixmap, pixmap.getFormat(), false, false, true));
                }
            });
        }

        @Override
        public void failed(Throwable t) {
            t.printStackTrace();
            Gdx.app.log("EmptyDownloadTest", "Failed", t);
        }

        @Override
        public void cancelled() {
            Gdx.app.log("EmptyDownloadTest", "Cancelled");
        }
    });
}
Also used : HttpRequest(com.badlogic.gdx.Net.HttpRequest) HttpResponse(com.badlogic.gdx.Net.HttpResponse) PixmapTextureData(com.badlogic.gdx.graphics.glutils.PixmapTextureData) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Texture(com.badlogic.gdx.graphics.Texture) Pixmap(com.badlogic.gdx.graphics.Pixmap) HttpResponseListener(com.badlogic.gdx.Net.HttpResponseListener)

Example 2 with HttpRequest

use of com.badlogic.gdx.Net.HttpRequest in project libgdx by libgdx.

the class HttpRequestBuilder method build.

/** Returns the {@link HttpRequest} that has been setup by this builder so far. After using the request, it should be returned
	 * to the pool via {@code Pools.free(request)}. */
public HttpRequest build() {
    validate();
    HttpRequest request = httpRequest;
    httpRequest = null;
    return request;
}
Also used : HttpRequest(com.badlogic.gdx.Net.HttpRequest)

Example 3 with HttpRequest

use of com.badlogic.gdx.Net.HttpRequest in project libgdx by libgdx.

the class HttpRequestExample method create.

@Override
public void create() {
    HttpRequest request = new HttpRequest(HttpMethods.GET);
    request.setUrl("http://libgdx.badlogicgames.com/nightlies/dist/AUTHORS");
    Gdx.net.sendHttpRequest(request, new HttpResponseListener() {

        @Override
        public void handleHttpResponse(HttpResponse httpResponse) {
            Gdx.app.log("HttpRequestExample", "response: " + httpResponse.getResultAsString());
        }

        @Override
        public void failed(Throwable t) {
            Gdx.app.error("HttpRequestExample", "something went wrong", t);
        }

        @Override
        public void cancelled() {
            Gdx.app.log("HttpRequestExample", "cancelled");
        }
    });
}
Also used : HttpRequest(com.badlogic.gdx.Net.HttpRequest) HttpResponse(com.badlogic.gdx.Net.HttpResponse) HttpResponseListener(com.badlogic.gdx.Net.HttpResponseListener)

Example 4 with HttpRequest

use of com.badlogic.gdx.Net.HttpRequest in project libgdx by libgdx.

the class NetAPITest method create.

@Override
public void create() {
    batch = new SpriteBatch();
    skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    font = new BitmapFont();
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    {
        statusLabel = new Label("", skin);
        statusLabel.setWrap(true);
        statusLabel.setWidth(Gdx.graphics.getWidth() * 0.96f);
        statusLabel.setAlignment(Align.center);
        statusLabel.setPosition(Gdx.graphics.getWidth() * 0.5f - statusLabel.getWidth() * 0.5f, 30f);
        statusLabel.setColor(Color.CYAN);
        stage.addActor(statusLabel);
    }
    {
        ClickListener clickListener = new ClickListener() {

            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                clickedButton = event.getListenerActor();
                setButtonDisabled(true);
                if (texture != null)
                    texture.dispose();
                texture = null;
                text = null;
                String url;
                String httpMethod = Net.HttpMethods.GET;
                String requestContent = null;
                if (clickedButton == btnDownloadImage)
                    url = "http://i.imgur.com/vxomF.jpg";
                else if (clickedButton == btnDownloadText)
                    url = "http://www.apache.org/licenses/LICENSE-2.0.txt";
                else if (clickedButton == btnDownloadLarge)
                    url = "http://libgdx.badlogicgames.com/releases/libgdx-1.2.0.zip";
                else if (clickedButton == btnDownloadError)
                    url = "http://www.badlogicgames.com/doesnotexist";
                else if (clickedButton == btnOpenUri) {
                    Gdx.net.openURI("http://libgdx.badlogicgames.com/");
                    return;
                } else {
                    url = "http://posttestserver.com/post.php?dump";
                    httpMethod = Net.HttpMethods.POST;
                    requestContent = "name1=value1&name2=value2";
                }
                httpRequest = new HttpRequest(httpMethod);
                httpRequest.setUrl(url);
                httpRequest.setContent(requestContent);
                Gdx.net.sendHttpRequest(httpRequest, NetAPITest.this);
                statusLabel.setText("Downloading data from " + httpRequest.getUrl());
            }
        };
        ClickListener cancelListener = new ClickListener() {

            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                if (httpRequest != null) {
                    Gdx.net.cancelHttpRequest(httpRequest);
                    Gdx.app.log("NetAPITest", "Cancelling request " + httpRequest.getUrl());
                    statusLabel.setText("Cancelling request " + httpRequest.getUrl());
                }
            }
        };
        btnCancel = new TextButton("Cancel", skin);
        btnCancel.setPosition(Gdx.graphics.getWidth() * 0.10f, 60f);
        btnCancel.addListener(cancelListener);
        stage.addActor(btnCancel);
        btnDownloadImage = new TextButton("GET Image", skin);
        btnDownloadImage.setPosition(btnCancel.getX() + btnCancel.getWidth() + 10, 60f);
        btnDownloadImage.addListener(clickListener);
        stage.addActor(btnDownloadImage);
        btnDownloadText = new TextButton("GET Text", skin);
        btnDownloadText.setPosition(btnDownloadImage.getX() + btnDownloadImage.getWidth() + 10, 60f);
        btnDownloadText.addListener(clickListener);
        stage.addActor(btnDownloadText);
        btnDownloadLarge = new TextButton("GET Large", skin);
        btnDownloadLarge.setPosition(btnDownloadText.getX() + btnDownloadText.getWidth() + 10, 60f);
        btnDownloadLarge.addListener(clickListener);
        stage.addActor(btnDownloadLarge);
        btnDownloadError = new TextButton("GET Error", skin);
        btnDownloadError.setPosition(btnDownloadLarge.getX() + btnDownloadLarge.getWidth() + 10, 60f);
        btnDownloadError.addListener(clickListener);
        stage.addActor(btnDownloadError);
        btnPost = new TextButton("POST", skin);
        btnPost.setPosition(btnDownloadError.getX() + btnDownloadError.getWidth() + 10, 60f);
        btnPost.addListener(clickListener);
        stage.addActor(btnPost);
        btnOpenUri = new TextButton("Open URI", skin);
        btnOpenUri.setPosition(btnPost.getX() + btnPost.getWidth() + 10, 60f);
        btnOpenUri.addListener(clickListener);
        stage.addActor(btnOpenUri);
    }
}
Also used : HttpRequest(com.badlogic.gdx.Net.HttpRequest) TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) Stage(com.badlogic.gdx.scenes.scene2d.Stage) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) ClickListener(com.badlogic.gdx.scenes.scene2d.utils.ClickListener)

Example 5 with HttpRequest

use of com.badlogic.gdx.Net.HttpRequest in project Mindustry by Anuken.

the class Net method http.

public static void http(String url, String method, Consumer<String> listener, Consumer<Throwable> failure) {
    HttpRequest req = new HttpRequestBuilder().newRequest().method(method).url(url).build();
    Gdx.net.sendHttpRequest(req, new HttpResponseListener() {

        @Override
        public void handleHttpResponse(HttpResponse httpResponse) {
            listener.accept(httpResponse.getResultAsString());
        }

        @Override
        public void failed(Throwable t) {
            failure.accept(t);
        }

        @Override
        public void cancelled() {
        }
    });
}
Also used : HttpRequest(com.badlogic.gdx.Net.HttpRequest) HttpResponse(com.badlogic.gdx.Net.HttpResponse) HttpRequestBuilder(com.badlogic.gdx.net.HttpRequestBuilder) HttpResponseListener(com.badlogic.gdx.Net.HttpResponseListener)

Aggregations

HttpRequest (com.badlogic.gdx.Net.HttpRequest)5 HttpResponse (com.badlogic.gdx.Net.HttpResponse)3 HttpResponseListener (com.badlogic.gdx.Net.HttpResponseListener)3 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 Texture (com.badlogic.gdx.graphics.Texture)1 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 PixmapTextureData (com.badlogic.gdx.graphics.glutils.PixmapTextureData)1 HttpRequestBuilder (com.badlogic.gdx.net.HttpRequestBuilder)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1 Stage (com.badlogic.gdx.scenes.scene2d.Stage)1 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)1 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)1 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)1 ClickListener (com.badlogic.gdx.scenes.scene2d.utils.ClickListener)1