Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons IO on Android

I'm developing an Android application that uses Apache Commons IO, commons-io-2.4-bin.tar.gz.

And I get some errors, one of them:

Could not find method java.lang.String.getBytes, referenced from method org.apache.commons.io.IOUtils.toInputStream

I think I don't have to worry about it, isn't it?

Is there another specific Android library that I can use instead of Apache Commons IO?

I'm using it here:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;

import android.util.Log;

public class CustomResponseErrorHandler implements ResponseErrorHandler
{

    private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();

    @Override
    public void handleError(ClientHttpResponse response) throws IOException
    {
        String theString = IOUtils.toString(response.getBody());
        Log.v("Error Handler", theString);
        CustomException exception = new CustomException();
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("code", response.getStatusCode().toString());
        properties.put("body", theString);
        properties.put("header", response.getHeaders());
        exception.setProperties(properties);

        throw exception;
    }

    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException
    {
        return errorHandler.hasError(response);
    }

}

On this line:

String theString = IOUtils.toString(response.getBody());
like image 906
VansFannel Avatar asked Jun 28 '13 07:06

VansFannel


People also ask

How do I install Apache Commons IO?

Download Common IO ArchiveDownload the latest version of Apache Common IO jar file from commons-io-2.11. 0-bin. zip. At the time of writing this tutorial, we have downloaded commons-io-2.11.

What is org Apache Commons IO?

Apache Commons IO is a library of utilities to assist with developing IO functionality. There are six main areas included: io - This package defines utility classes for working with streams, readers, writers and files. comparator - This package provides various Comparator implementations for Files.

Why Commons IO jar is used?

Commons IO includes a list of useful file filters. These can come in handy when a developer wants to narrow down to a specific desired list of files from a heterogeneous list of files. The library also supports AND and OR logic operations on a given file list.


2 Answers

I recommend using the Apache Commons IO library source, and then packaging it to Android :) I have packaged it in a repo here. Just import it into your IDE of choice!

like image 174
Steven Mann Avatar answered Sep 17 '22 12:09

Steven Mann


Guava might be a good replacement for you. Their wiki claims they're fully compatible with Android:

Guava 11.0.2 and earlier are fully compatible with Android, but more recent Android versions may be required for the latest Guava releases. In particular, NavigableSet and other extensions added in Java 6 (used by Guava 12 and above) were added in Android API version 9, corresponding with Gingerbread.

(Android programmers concerned with Guava's large JAR size are advised to use ProGuard to get only the parts of Guava they need. Guava is one of the most common dependencies for Android applications.)

For the Guava equivalent of IOUtils.toString() check the following question: Guava equivalent for IOUtils.toString(InputStream)

like image 45
Jasper Avatar answered Sep 20 '22 12:09

Jasper