tags:

views:

110

answers:

1

Why import org.apache.commons.lang.StringUtils cannot be imported in android by default.

Do i have to include an external library? Then where can i find that library on the web?

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;
        }
}
+2  A: 

Apache Commons lang is a separate library. You can find it here.

wds
I have it downloaded already. The downloaded folder does not have any jar file that i can include in my android project. What to do now?
Maxood
The jar files are inside this zip file: http://apache.mogo.be/commons/lang/binaries/commons-lang-2.5-bin.zip
wds
Ok! Downloaded and ran the code but i'm getting null pointer exceptionwhen i call the encode method in my main activity?
Maxood
@Maxood I think that would be an unrelated problem to this question. Without looking at your main method I can't say what might be the issue. Are you sure you fill in the `id` value? Perhaps ask another question about this specific problem if you can't figure it out.
wds
Here is my main activity:import android.app.Activity;import android.os.Bundle;public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); long var = 70; Long multiplier=Long.parseLong("1zzzz",36); EncodeIdUtil edUtil = new EncodeIdUtil(); //Null pointer exception edUtil.encode(var); }}
Maxood