3

My current code using these methods is here and work fine for getting text back. How can I modify this to save an image to the filesystem?

public static class URLResponseListener implements HttpResponseListener {
    public void handleHttpResponse(HttpResponse httpResponse) {
        statusCode = httpResponse.getStatus().getStatusCode();
        urlResults = httpResponse.getResultAsString();
    }

    public void failed(Throwable t) {
        statusCode = Constants.CONNECTION_FAILED;
        urlResults = null;
    }

    public String getLastResponse() {
        return urlResults;
    }

    @Override
    public void cancelled() {
        urlResults = null;          
    }
}

private void getUrlData() {
    httpGet = new HttpRequest(HttpMethods.GET);
    httpGet.setUrl(Constants.DATA_URL);
    responseListener = new URLResponseListener();
    Gdx.net.sendHttpRequest (httpGet, responseListener);
}

In my render method I have the following check for when the http connection has completed

        if (statusCode >= 200 && statusCode <= 300 && null != urlResults) {
            try {
            // process urlResults text
            } catch (Exception e) {
                e.printStackTrace();
            }
            statusCode = 0;
            urlResults = null;

        }
        if (statusCode == Constants.RETRY_CONNECTION || statusCode == Constants.CONNECTION_FAILED) {
            statusCode = 0;
            getUrlData();
        }
Sym
  • 187
  • 2
  • 9

1 Answers1

0

Got the answer from BurningHand on the libgdx forum

@Override
public void handleHttpResponse (HttpResponse httpResponse) {
    if (httpResponse.getStatus().getStatusCode() == 200) {
        Gdx.files.external("downloaded.img").write(httpResponse.getResultAsStream(), false);
    }
}
Sym
  • 187
  • 2
  • 9