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

时间:2010-05-11 07:18:29

标签: 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;
        }
}

5 个答案:

答案 0 :(得分:29)

您不会说您是使用Eclipse还是Android Studio。在Android Studio中,您可以添加

import org.apache.commons.lang3.StringUtils;

到你的源代码文件。在build.gradle中,您需要更改依赖项,例如

dependencies {
    compile 'com.android.support:support-v4:+'
}

dependencies {
    compile 'com.android.support:support-v4:+'
    compile  'org.apache.commons:commons-lang3:3.0'
}

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

答案 1 :(得分:10)

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

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

答案 2 :(得分:5)

Apache Commons lang是一个单独的库。你可以找到它here

答案 3 :(得分:2)

如果您使用的是Android Studio,请尝试使用提供的工作流程来添加依赖项,而不是手动添加下载的库或手动更改gradle文件。

  1. 文件->项目结构->模块->应用->依赖项标签
  2. 点击左下角的“ +”,然后选择“库依赖项”
  3. 在搜索字段中输入:“ org.apache.commons:commons-lang3”,然后单击“搜索”
  4. 选择“ org.apache.commons:commons-lang3:3.7”

enter image description here

答案 4 :(得分:0)

只需在模块级gradle文件(build.gradle)中添加以下依赖项

function arabic_w2e($str){
 $arabic_eastern = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩');
 $arabic_western = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
return str_replace($arabic_western, $arabic_eastern, $str);
}