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

查看:215
本文介绍了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());

推荐答案

番石榴可能很好为您更换. 他们的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版本.特别是,在Java 6中添加的NavigableSet和其他扩展(由Guava 12及更高版本使用)已在Android API版本9中添加,对应于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()的番石榴,请检查以下问题:等效于IOUtils.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天全站免登陆