Search in sources :

Example 1 with HttpResponseListener

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");
        }
    });
}
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 HttpResponseListener

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");
        }
    });
}
Also used : HttpRequest(com.badlogic.gdx.Net.HttpRequest) HttpResponse(com.badlogic.gdx.Net.HttpResponse) HttpResponseListener(com.badlogic.gdx.Net.HttpResponseListener)

Example 3 with HttpResponseListener

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);
    }
}
Also used : HttpResponseListener(com.badlogic.gdx.Net.HttpResponseListener)

Example 4 with HttpResponseListener

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;
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) URL(java.net.URL) IOException(java.io.IOException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) HttpURLConnection(java.net.HttpURLConnection) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) ObjectMap(com.badlogic.gdx.utils.ObjectMap) HttpResponseListener(com.badlogic.gdx.Net.HttpResponseListener)

Aggregations

HttpResponseListener (com.badlogic.gdx.Net.HttpResponseListener)4 HttpRequest (com.badlogic.gdx.Net.HttpRequest)2 HttpResponse (com.badlogic.gdx.Net.HttpResponse)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 Texture (com.badlogic.gdx.graphics.Texture)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 PixmapTextureData (com.badlogic.gdx.graphics.glutils.PixmapTextureData)1 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)1 ObjectMap (com.badlogic.gdx.utils.ObjectMap)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Map (java.util.Map)1