Android 上的 Apache Commons IO [英] Apache Commons IO on Android

查看:22
本文介绍了Android 上的 Apache Commons IO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

推荐答案

Guava 可能是一个不错的选择替你换.他们的 wiki 声称他们与 Android 完全兼容:

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

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

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.

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

(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.)

对于 IOUtils.toString() 的 Guava 等效项,请检查以下问题:IOUtils 的 Guava 等效项.toString(InputStream)

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

这篇关于Android 上的 Apache Commons IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆