Monday, May 7, 2012

isAlpha function in JAVA

This function will return true. if a string contain character value.

like String s="123 Pawan";

public static boolean isAlpha(String s)
{
boolean check=false;
int p=0;

String[] _sc =
{
"a","b","c","d","e","f","g","h","i","j",
"k","l","m","n","o","p","q","r","s","t",
"u","v","w","x","y","z",

"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z",

"~","!","@","#","$","%","^","&",
"*","(",")","-","_","+","=",
",",".","/","?","<",">",":",";","'","|","\",","]","}","[","{","*",
};
for(int i=0;i<_sc.length;i++)
{
String sp=_sc[i];
if(s.indexOf(sp)!=-1)
{
p++;
break;
}
}
if(p!=0)
{
check=true;
}
return check;
}

Wednesday, May 2, 2012

Download gif image from server in BlackBerry


public static Bitmap downloadImage()
     {
           InputStream iStream = null;
           EncodedImage bitmap;
           Bitmap bmp = null ;
           HttpConnection httpConnection = null;

           try
           {
                httpConnection = (HttpConnection) Connector.open("http://www.google.com/ig/images/weather/mostly_cloudy.gif"
                           +ConnectionInfo.getInstance().getConnectionParameters(), Connector.READ_WRITE);

                httpConnection.setRequestMethod(HttpConnection.GET);
                int responseCode = httpConnection.getResponseCode();
                if (responseCode == HttpConnection.HTTP_OK) {

                     iStream = httpConnection.openInputStream();

                     byte[] imageData =  IOUtilities.streamToBytes(iStream);
                     bitmap = EncodedImage.createEncodedImage(imageData, 0, imageData.length);
                     bmp = bitmap.getBitmap();
                     Dialog.alert(""+bmp);
                }
           }
           catch (Exception e)
           {   
                System.out.println("error:"+e.getMessage());
           }
           return bmp;                 
     }