Posts

Showing posts from August, 2013

How do you encode URL in Android ?

Encoding URL is very straightforward, simply call the encode method of the URL Encoder class on the String data that you want to encode. try {     String url = "http://www.yoursite.com/blah";   String queryString = "param=1&value=2";     String encodedQueryString = URLEncoder.encode(queryString,"UTF-8");   String encodedQueryStringInBase64 = Base64.encodeToString(encodedQueryString, Base64.DEFAULT);    encodedUrl = url + "?" + encodedQueryStringInBase64;    Log.d("Enc URL: " + encodedurl);  }  catch (UnsupportedEncodingException e)  {   Log.e("Exception: " + e.getMessage());  } First: Choose an encoding (UTF-8 is generally a good choice) Transmitting end: Encode the string to bytes (e.g. text.getBytes(encodingName)) Encode the bytes to base64 using the Base64 class Transmit the base64 Receiving end: Receive the base64 Decode the base64 to bytes using the Base64 class Decode the ...

Always reuse immutable constant objects for better memory utilization

Severity:   Medium Rule:   Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption. Reason:   Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption. Usage Example:  public class Test {   protected Object[] getObjects()   {   return new Object[0];  // VIOLATION   }    publicstatic Integer convertToInt(String s)   {   if (s == null || s.length() == 0)   { return new Integer(-1);  // VIOLATION }   else   { return ne...

Use string length to compare empty string variables

Severity:   High Rule:   The String.equals() method is overkill to test for an empty string. It is quicker to test if the length of the string is 0. Reason:   The String.equals() method is overkill to test for an empty string. It is quicker to test if the length of the string is 0. Usage Example:  package com.rule; class Use_String_length_to_compare_empty_string_violation { public boolean isEmpty(String str) { return str.equals(""); // VIOLATION } } Should be written as: package com.rule; class Use_String_length_to_compare_empty_string_correction { public boolean isEmpty(String str) { return str.length()==0; // ...