Showing posts with label Blackberry. Show all posts
Showing posts with label Blackberry. Show all posts

Tuesday, 11 June 2013

Blackberry: Post Data Using JSON Object

We can post the data by the JSON format. 
For this first,
1. We have to take all the keys and values in One Hash table or Array of Hash tables(Vector)
2. Convert it into JSON object.
3. Send to server.
packagecom.alishaik.thread;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.Hashtable;
importjavax.microedition.io.Connector;
importjavax.microedition.io.HttpConnection;
importnet.rim.blackberry.api.browser.URLEncodedPostData;
importnet.rim.device.api.io.http.HttpProtocolConstants;
importnet.rim.device.api.ui.Screen;
importnet.rim.device.api.ui.UiApplication;
importcom.hiddenbrains.ddft.common.CommonMethods;
importcom.hiddenbrains.ddft.common.MessageScreen;
importcom.hiddenbrains.ddft.common.Utility;
importcom.hiddenbrains.ddft.json.JSONObject;
public class PostDataThread extendsThread
{
    privateHttpConnection httpConnection;
    privateInputStream inputStream;
    privateStringBuffer stringBuffer=newStringBuffer();
    privateHashtable hashTable;
    privateString URL="";
    privateintindex=0;
     
      
    public PostDataThread(String url, Hashtable hash,intindex) 
    {
//      System.out.println("=========WEB SERVICE===========: "+url);        
        URL=Utility.escapeHTML(url)+CommonMethods.updateConnectionSuffix();//interface=wifi; or deviceside=false;
        this.index=index;
        this.hashTable=hash;//Here I am taking one Hashtable. You can manage according to your requirement.
    }
     
    publicvoidrun() 
    {
        try
        {
            httpConnection=(HttpConnection)Connector.open(URL);
            httpConnection.setRequestMethod(HttpConnection.POST);
             
            URLEncodedPostData oPostData =newURLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
            oPostData.setData(provideJSONObject());//IMPORTANT
             
            byte[] postBytes = oPostData.getBytes();
            httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,"application / requestJson");
            httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
            
            OutputStream strmOut = httpConnection.openOutputStream();
            strmOut.write(postBytes);
            strmOut.flush();
            strmOut.close();
             
            intresponse=httpConnection.getResponseCode();
            if(response==HttpConnection.HTTP_OK)
            {
                inputStream = httpConnection.openInputStream();
                intc;
                while((c=inputStream.read())!=-1)
                {
                    stringBuffer.append((char)c);
                }                   
                parseData(stringBuffer.toString());             
            }
            else
                parserInterface.didFailParser("Connection failure HTTP Response: "+response);
        }
        catch(finalException e) 
        {
            String msg=e.getMessage();
            if(msg==null)
                msg="No Data Found";
            //Show failure alert here
        }
        finally
        {
            try
            {
                if(httpConnection!=null)
                    httpConnection.close();
                if(inputStream!=null)
                    inputStream.close();
            
            catch(Exception e2) 
            {}          
        }
    }
     
    private String provideJSONObject()
    {
        try
        {
            JSONObject jsonObj=newJSONObject();
            jsonObj.put("data", hashTable);//Getting correct data; You can check here
//          System.out.println("======================JSON STRING FOR POSTING: "+jsonObj.toString());
            returnjsonObj.toString();
        
        catch(Exception e) 
        {
            returnnull;
        }
    }
     
    private void parseData(String responseString) 
    {
        responseString="["+responseString+"]";
        System.out.println("======================\n\n"+responseString+"\n\n===================");
        //Do what you want.        
    }
}
and the method for Utility.escapeHTML(url) is:

public static String escapeHTML(String s)
{
               StringBuffer sb = new StringBuffer();
               int n = s.length();
               for (int i = 0; i < n; i++) {
                     char c = s.charAt(i);
                      switch (c) {
                                           case ‘ ‘: sb.append(“%20″); break;
                                         default: sb.append(c); break;
                     }
             }
            return sb.toString();
}
You can get this class by this below link:

Sunday, 9 June 2013

Blackberry: Video Recording in Blackberry

For video recording first we have to check the memory available in Blackberry. According to that we have to go to the next step.
Download the zip file for the example for Video recording in Blackberry

Thursday, 12 January 2012

Create Splash Screen in Blackberry

--> The below code shows the Spalsh screen means at the starting time of application you can show some logo/Client's Image for few seconds and then go to the application's main Screen;


//================ StartUp.java=========
 public class StartUp extends UiApplication
{
    public static void main(String[]ali)
    {
        StartUp start=new StartUp();
        start.enterEventDispatcher();
    }
    public StartUp()
    {
        this.pushScreen(new SplashScreen());
        invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    Thread.sleep(2000);
                    pushScreen(new FirstScreen());
                }
                catch (Exception e)
                {
                    exceptionHandling(e.getMessage());
                }
            }
        });
      
    }
  
    public static void exceptionHandling(final String exception)
    {
        UiApplication.getUiApplication().invokeLater(new Runnable()
        {      
            public void run()
            {
                Dialog.alert(exception);
            }
        });
    }
}

//================ SplashScreen.java=========

public class SplashScreen extends MainScreen
{
    Bitmap bitmap=Bitmap.getBitmapResource("loading-screen.png");//This is my company logo;
    BitmapField loadingImage=new BitmapField(bitmap);
    public SplashScreen()
    {
        createGUI();
    }

    private void createGUI()
    {
        try
        {
            VerticalFieldManager vertical=new VerticalFieldManager()
            {
                protected void paint(Graphics g)
                {
                    g.drawBitmap(0, 0,bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);
                    super.paint(g);
                }
                protected void sublayout(int maxWidth, int maxHeight)
                {
                    super.sublayout(Display.getWidth(),Display.getHeight());
                    setExtent(Display.getWidth(),Display.getHeight());
                }
            };
           
//            Nothing to write;
//            Here you can check the some network connections;
//            do somthing;
           
            add(vertical);
        }
        catch (Exception e)
        {
            StartUp.exceptionHandling(e.getMessage());
        }
    }
}


//================ FirstScreen .java========= 

public class FirstScreen extends MainScreen
{       
    VerticalFieldManager vertical;   
   
    public FirstScreen()
    {               
        createGUI();
    }
   
    private void createGUI()
    {
        setTitle("Loading Screen");
        vertical=new VerticalFieldManager()
        {
            protected void sublayout(int maxWidth, int maxHeight)
            {
                super.sublayout(Display.getWidth(),Display.getHeight());
                setExtent(Display.getWidth(),Display.getHeight());
            }
        };
        add(vertical);
    }
   
    public boolean onMenu(int instance)
    {
        return true;
    }
}