为什么在Android中无法识别StringUtils Apache类?

Muh*_*man 12 android

为什么导入org.apache.commons.lang.StringUtils默认情况下不能在android中导入.

我是否必须包含外部库?那我在哪里可以找到网上的图书馆?

package com.myapps.urlencoding;

import android.app.Activity;
import org.apache.commons.lang.StringUtils;

public class EncodeIdUtil extends Activity {
    /** Called when the activity is first created. */
     private static Long multiplier=Long.parseLong("1zzzz",36);

        /**
         * Encodes the id.
         * @param id the id to encode
         * @return encoded string
         */
        public static String encode(Long id) {
            return StringUtils.reverse(Long.toString((id*multiplier), 35));
        }

        /**
         * Decodes the encoded id.
         * @param encodedId the encodedId to decode
         * @return the Id
         * @throws IllegalArgumentException if encodedId is not a validly encoded id.
         */
        public static Long decode(String encodedId) 
            throws IllegalArgumentException {
            long product;
            try {
                product = Long.parseLong(StringUtils.reverse(encodedId), 35);
            } catch (Exception e) {
                throw new IllegalArgumentException();
            }
            if ( 0 != product % multiplier || product < 0) {
                throw new IllegalArgumentException();
            }
            return product/multiplier;
        }
}
Run Code Online (Sandbox Code Playgroud)

Vec*_*tec 30

您没有说您是使用Eclipse还是Android Studio.在Android Studio中,您可以添加,

import org.apache.commons.lang3.StringUtils;
Run Code Online (Sandbox Code Playgroud)

到您的源代码文件.在build.gradle中,你需要改变你的依赖关系,比如

dependencies {
    compile 'com.android.support:support-v4:+'
}
Run Code Online (Sandbox Code Playgroud)

dependencies {
    compile 'com.android.support:support-v4:+'
    compile  'org.apache.commons:commons-lang3:3.0'
}
Run Code Online (Sandbox Code Playgroud)

换句话说,您将添加到依赖项.


Ale*_*lex 10

Android在android.text.TextUtils中提供了该功能的子集.

根据您对StringUtils的需求,这可能是一个选项.例如,它有加入,分裂.


wds*_*wds 5

Apache Commons lang是一个独立的库.你可以在这里找到它.