use of com.badlogic.gdx.Net.HttpResponseListener 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.HttpResponseListener 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.HttpResponseListener in project libgdx by libgdx.
the class NetJavaImpl method cancelHttpRequest.
public void cancelHttpRequest(HttpRequest httpRequest) {
HttpResponseListener httpResponseListener = getFromListeners(httpRequest);
if (httpResponseListener != null) {
httpResponseListener.cancelled();
removeFromConnectionsAndListeners(httpRequest);
}
}
use of com.badlogic.gdx.Net.HttpResponseListener in project libgdx by libgdx.
the class NetJavaImpl method sendHttpRequest.
public void sendHttpRequest(final HttpRequest httpRequest, final HttpResponseListener httpResponseListener) {
if (httpRequest.getUrl() == null) {
httpResponseListener.failed(new GdxRuntimeException("can't process a HTTP request without URL set"));
return;
}
try {
final String method = httpRequest.getMethod();
URL url;
if (method.equalsIgnoreCase(HttpMethods.GET)) {
String queryString = "";
String value = httpRequest.getContent();
if (value != null && !"".equals(value))
queryString = "?" + value;
url = new URL(httpRequest.getUrl() + queryString);
} else {
url = new URL(httpRequest.getUrl());
}
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// should be enabled to upload data.
final boolean doingOutPut = method.equalsIgnoreCase(HttpMethods.POST) || method.equalsIgnoreCase(HttpMethods.PUT);
connection.setDoOutput(doingOutPut);
connection.setDoInput(true);
connection.setRequestMethod(method);
HttpURLConnection.setFollowRedirects(httpRequest.getFollowRedirects());
putIntoConnectionsAndListeners(httpRequest, httpResponseListener, connection);
// Headers get set regardless of the method
for (Map.Entry<String, String> header : httpRequest.getHeaders().entrySet()) connection.addRequestProperty(header.getKey(), header.getValue());
// Set Timeouts
connection.setConnectTimeout(httpRequest.getTimeOut());
connection.setReadTimeout(httpRequest.getTimeOut());
asyncExecutor.submit(new AsyncTask<Void>() {
@Override
public Void call() throws Exception {
try {
// Set the content for POST and PUT (GET has the information embedded in the URL)
if (doingOutPut) {
// we probably need to use the content as stream here instead of using it as a string.
String contentAsString = httpRequest.getContent();
if (contentAsString != null) {
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
try {
writer.write(contentAsString);
} finally {
StreamUtils.closeQuietly(writer);
}
} else {
InputStream contentAsStream = httpRequest.getContentStream();
if (contentAsStream != null) {
OutputStream os = connection.getOutputStream();
try {
StreamUtils.copyStream(contentAsStream, os);
} finally {
StreamUtils.closeQuietly(os);
}
}
}
}
connection.connect();
final HttpClientResponse clientResponse = new HttpClientResponse(connection);
try {
HttpResponseListener listener = getFromListeners(httpRequest);
if (listener != null) {
listener.handleHttpResponse(clientResponse);
}
removeFromConnectionsAndListeners(httpRequest);
} finally {
connection.disconnect();
}
} catch (final Exception e) {
connection.disconnect();
try {
httpResponseListener.failed(e);
} finally {
removeFromConnectionsAndListeners(httpRequest);
}
}
return null;
}
});
} catch (Exception e) {
try {
httpResponseListener.failed(e);
} finally {
removeFromConnectionsAndListeners(httpRequest);
}
return;
}
}
Aggregations