2010-05-11 2 views
6

이유 가져 오기 org.apache.commons.lang.StringUtils은 기본적으로 android에서 가져올 수 없습니다.Android에서 StringUtils Apache 클래스가 인식되지 않는 이유는 무엇입니까?

외부 라이브러리를 포함해야합니까? 그렇다면 웹에서 라이브러리를 어디에서 찾을 수 있습니까?

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; 
     } 
} 
+0

필요한 API를 다운로드해야하는 사이트입니까? http://commons.apache.org/lang/download_lang.cgi –

답변

5

Apache Commons lang은 별도의 라이브러리입니다. 당신은 그것을 here 찾을 수 있습니다.

+0

이미 다운로드했습니다. 다운로드 한 폴더에는 내 안드로이드 프로젝트에 포함시킬 수있는 jar 파일이 없습니다. 지금 무엇을해야할까요? –

+0

jar 파일은이 zip 파일 안에 있습니다. http://apache.mogo.be/commons/lang/binaries/commons-lang-2.5-bin.zip – wds

+0

Ok! 다운로드 및 코드를 실행하지만 널 포인터 예외가 발생했습니다 내 주요 활동에서 인코딩 방법을 호출 할 때? –

7

Android는 해당 기능의 하위 집합을 android.text.TextUtils에 제공합니다.

StringUtils에서 필요한 항목에 따라 옵션이 될 수 있습니다. 예 : 참여가 분할되었습니다.

22

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' 
} 

에, 같은에서 종속성을 변경해야합니다, 당신은 의존성에 추가합니다.

+0

을 원하는 jar 파일 옆에 다음 순서와 수출, 다음의 체크 박스를 클릭하여 빌드 경로를 구성 이동하려면 응용 프로그램을 패키지로하지 않기 때문에 당신은 아마도 널 포인터 예외를 얻고있다 이 라이브러리를 사용하지 마십시오. 이 lib를 사용하면 apk를 Google Play 스토어에 업로드하는 동안 지원되는 기기가 0 개 표시됩니다. 대신 'commons-lang : commons-lang : 2.6'을 컴파일하십시오. 그것은 작동합니다. – vikoo

관련 문제