【问题标题】:Why StringUtils Apache class is not recognized in Android?为什么 Android 中无法识别 StringUtils Apache 类?
【发布时间】:2019-11-07 17:17:04
【问题描述】:

为什么import 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;
        }
}

【问题讨论】:

标签: android


【解决方案1】:

您没有说您使用的是 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'
}

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

【讨论】:

  • 不要使用这个库。通过使用此库,在将 apk 上传到 Google Play 商店时,它将显示 0 个支持的设备。而是使用 compile 'commons-lang:commons-lang:2.6'。它有效。
【解决方案2】:

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

根据您对 StringUtils 的需求,这可能是一种选择。例如,它有join、split。

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 我已经下载了。下载的文件夹没有我可以包含在我的 android 项目中的任何 jar 文件。现在该怎么办?
    • 好的!下载并运行代码,但在我的主要活动中调用编码方法时出现空指针异常?
    • @Maxood 我认为这与这个问题无关。如果不看你的主要方法,我不能说可能是什么问题。您确定填写id 值吗?如果您无法弄清楚,也许可以就这个特定问题提出另一个问题。
    • 这是我的主要活动:import android.app.Activity;导入android.os.Bundle; public class MyActivity extends Activity { /** 首次创建活动时调用。 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);设置内容视图(R.layout.main);长变量 = 70;长乘数=Long.parseLong("1zzzz",36); EncodeIdUtil edUtil = new EncodeIdUtil(); //空指针异常 edUtil.encode(var); } }
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

      implementation  'org.apache.commons:commons-lang3:3.7'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多