Encode a URL in Android with a Utility Choreo


Temboo does a lot more than just work with APIs. We've got a growing collection of programming utilities that will make your life much easier. Here we'll show you how to use these handy code shortcuts by working through a URL encoding example.

You might ask "why is URL encoding important?". Well, URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain non-ASCII characters, they need to be converted so they only contain valid characters. The general rule is that unsafe characters are replaced with a % followed by two hexadecimal digits. Spaces are replaced by either a plus sign (+) or with %20. You've probably seen URLs full of strange looking characters, and now you know why!

The good news is that you don't actually have to worry about the specifics. Our URL encoding utility will do all the work for you.

Get Set Up

1 Log in to Temboo. If you don't already have an account, you can register for free.

2Download the Temboo Android SDK and add it to your development environment. Confirm that your project has <uses-permission android:name="android.permission.INTERNET"/> in its manifest. Clear the text in the generated TextArea, and set its id to @+id/result. If you need help with any of these steps, please refer to our Android getting started tutorial, but copy and import the Utilities-1.76.jar instead of the Google-1.76.jar.

Test the Choreo

3Go to the Utilities > Encoding > URLEncode Choreo in our library. Select Android from the drop down menu at the top of the page.

4Enter some text that you want to URL encode. It should contain illegal characters, like spaces, accents, and punctuation. Here's some text you can use:

I'm Enc0ding URL$ wITh T3mBo0

5 Click Generate Code to test the Choreo from our website. You'll see the the URL encoded result returned by the Choreo.

Create Your Android Program

6To call the URL encoding Choreo from your Android program, copy the generated Code from your browser directly into the doInBackground method of your AsyncTask class and try running it. Your code should look something like this:

package com.temboo.android.gettingstarted.app;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

import com.temboo.Library.Utilities.Encoding.URLEncode;
import com.temboo.Library.Utilities.Encoding.URLEncode.*;
import com.temboo.core.TembooSession;

/**
 * An AsyncTask that uses Temboo to url encode a string
 * results from Youtube.
 */
class Utilities extends AsyncTask<Void, Void, String> {

    private TextView textView;

    public Utilities(TextView textView){
        this.textView = textView;
    }

    @Override
    protected String doInBackground(Void... arg0) {
        try {
            TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

            URLEncode uRLEncodeChoreo = new URLEncode(session);

            // Get an InputSet object for the choreo
            URLEncodeInputSet uRLEncodeInputs = uRLEncodeChoreo.newInputSet();

            // Set inputs
            uRLEncodeInputs.set_Text("I'm Enc0ding URL$ wITh T3mBo0");

            // Execute Choreo
            URLEncodeResultSet uRLEncodeResults = uRLEncodeChoreo.execute(uRLEncodeInputs);
            
            // Print Results
            return "URL encoded text: " + uRLEncodeResults.get_URLEncodedText();
        } catch(Exception e) {
            // if an exception occurred, log it
            Log.e(this.getClass().toString(), e.getMessage());
        }
        return null;
    }

    protected void onPostExecute(String result) {
        try {
            // Update UI
            textView.setText(result);
        } catch(Exception e) {
            // if an exception occurred, show an error message
            Log.e(this.getClass().toString(), e.getMessage());
        }
    }
}

If you have questions about how to invoke DropboxTask in your MainActivity class, refer to our getting started tutorial, but find the correct TextView with TextView textView = (TextView) findViewById(R.id.result);.

7Run the app and the URL encoded version of the text will be printed in the device screen.

What next?

We're all finished! Now you know about Temboo's range of handy developer utilities and how to access them via our Android SDK. Check out the rest of the 2000+ Choreos in our Library and see how you can combine them with our utilities to build something amazing.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back