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");
}
});
}
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;
}
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");
}
});
}
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);
}
}
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() {
}
});
}
Aggregations