5

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

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

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

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

我可以使用另一个特定的 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);
    }

}

在这条线上:

String theString = IOUtils.toString(response.getBody());
4

2 回答 2

3

我建议使用 Apache Commons IO 库源,然后将其打包到 Android :) 我已将其打包在此处的 repo 中。只需将其导入您选择的 IDE!

于 2014-07-17T22:01:40.717 回答
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 equivalent for IOUtils.toString(InputStream)

于 2013-06-28T07:23:50.053 回答