Code example for ByteArrayEntity

Methods: setContentType

0
        callMethod(new HttpGet(feedUrl), operation);
    } 
 
    public void post(String feedUrl, byte[] data, String contentType, Operation operation) throws IOException {
        ByteArrayEntity entity = getCompressedEntity(data);
        entity.setContentType(contentType);
        HttpPost post = new HttpPost(feedUrl);
        post.setEntity(entity);
        callMethod(post, operation);
    } 
 
    public void put(String feedUrl, byte[] data, String contentType, Operation operation) throws IOException {
        ByteArrayEntity entity = getCompressedEntity(data);
        entity.setContentType(contentType);
        HttpPost post = new HttpPost(feedUrl);
        post.setHeader(X_HTTP_METHOD_OVERRIDE, "PUT");
        post.setEntity(entity);
        callMethod(post, operation);
    } 
 
    public void putStream(String feedUrl, InputStream stream, String contentType, Operation operation) throws IOException {
        InputStreamEntity entity = new InputStreamEntity(stream, -1);
        entity.setContentType(contentType);
        HttpPost post = new HttpPost(feedUrl);
        post.setHeader(X_HTTP_METHOD_OVERRIDE, "PUT");
        post.setEntity(entity);
        callMethod(post, operation);
    } 
 
    public void delete(String feedUrl, Operation operation) throws IOException {
        HttpPost post = new HttpPost(feedUrl);
        String etag = operation.inOutEtag;
        post.setHeader(X_HTTP_METHOD_OVERRIDE, "DELETE");
        post.setHeader(IF_MATCH, etag != null ? etag : "*");
        callMethod(post, operation);
    } 
 
    private void callMethod(HttpUriRequest request, Operation operation) throws IOException {
        try { 
 
            // Specify GData protocol version 2.0. 
            request.addHeader("GData-Version", "2");
 
            // Indicate support for gzip-compressed responses. 
            request.addHeader("Accept-Encoding", "gzip");
 
            // Specify authorization token if provided. 
            String authToken = mAuthToken;
            if (!TextUtils.isEmpty(authToken)) {
                request.addHeader("Authorization", "GoogleLogin auth=" + authToken);
            } 
 
            // Specify the ETag of a prior response, if available. 
            String etag = operation.inOutEtag;
            if (etag != null) {
                request.addHeader("If-None-Match", etag);
            } 
 
            // Execute the HTTP request. 
            HttpResponse httpResponse = null;
            try { 
                httpResponse = mHttpClient.execute(request);
            } catch (IOException e) {
                Log.w(TAG, "Request failed: " + request.getURI());
                throw e;
            } 
 
            // Get the status code and response body. 
            int status = httpResponse.getStatusLine().getStatusCode();
            InputStream stream = null;
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                // Wrap the entity input stream in a GZIP decoder if necessary. 
                stream = entity.getContent();
                if (stream != null) {
                    Header header = entity.getContentEncoding();
                    if (header != null) {
                        if (header.getValue().contains("gzip")) {
                            stream = new GZIPInputStream(stream);
                        } 
                    } 
                } 
            } 
 
            // Return the stream if successful. 
            Header etagHeader = httpResponse.getFirstHeader("ETag");
            operation.outStatus = status;
            operation.inOutEtag = etagHeader != null ? etagHeader.getValue() : null;
            operation.outBody = stream;
 
        } catch (java.lang.IllegalStateException e) {
            Log.e(TAG, "Unhandled IllegalStateException e: "+ e);
            throw new IOException("Unhandled IllegalStateException");
        } 
    } 
 
    private ByteArrayEntity getCompressedEntity(byte[] data) throws IOException {
        ByteArrayEntity entity;
        if (data.length >= MIN_GZIP_SIZE) {
            ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(data.length / 2);
            GZIPOutputStream gzipOutput = new GZIPOutputStream(byteOutput);
            gzipOutput.write(data);
            gzipOutput.close();
            entity = new ByteArrayEntity(byteOutput.toByteArray());
        } else { 
            entity = new ByteArrayEntity(data);
        } 
        return entity;
    } 
Stop searching for code, let great code find you!  Add Codota to your java IDE