Android 上的 Apache Commons IO

Van*_*nel 5 java android apache-commons

我正在开发一个使用Apache Commons IO的 Android 应用程序,commons-io-2.4-bin.tar.gz

我得到了一些错误,其中之一:

Could not find method java.lang.String.getBytes, referenced from method org.apache.commons.io.IOUtils.toInputStream
Run Code Online (Sandbox Code Playgroud)

我想我不必担心,不是吗?

我可以使用另一个特定的 Android 库来代替 Apache Commons IO 吗?

我在这里使用它:

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

}
Run Code Online (Sandbox Code Playgroud)

在这一行:

String theString = IOUtils.toString(response.getBody());
Run Code Online (Sandbox Code Playgroud)

Jas*_*per 2

番石榴可能是你的一个很好的替代品。他们的 wiki声称他们与 Android 完全兼容:

Guava 11.0.2 及更早版本与 Android 完全兼容,但最新的 Guava 版本可能需要更新的 Android 版本。特别是,Java 6 中添加的 NavigableSet 和其他扩展(Guava 12 及以上版本使用)均在 Android API 版本 9 中添加,与 Gingerbread 相对应。

(担心 Guava 较大 JAR 大小的 Android 程序员建议使用 ProGuard 来仅获取他们需要的 Guava 部分。Guava 是 Android 应用程序最常见的依赖项之一。)

对于 Guava 等价物,IOUtils.toString()请检查以下问题:Guava 等价于 IOUtils.toString(InputStream)