BlackBerry - App freezes when background thread executing

I have a BlackBerry App that sends data over a web service when a button has it state set to ON. When the button is ON a timer is started which is running continuously in the background at fixed intervals. The method for HttpConnection is called as follows:

if(C0NNECTION_EXTENSION==null)
{
Dialog.alert("Check internet connection and try again");
return;
}
else
{
confirmation=PostMsgToServer(encryptedMsg); 
}

The method PostMsgToServer is as follows:

public static String PostMsgToServer(String encryptedGpsMsg)  { 

            //httpURL=  "https://prerel.track24c4i.com/track24prerel/service/spi/post?access_id="+DeviceBoardPassword+"&IMEI="+IMEI+"&hex_data="+encryptedGpsMsg+"&device_type=3";
        httpURL=  "https://t24.track24c4i.com/track24c4i/service/spi/post?access_id="+DeviceBoardPassword+"&IMEI="+IMEI+"&hex_data="+encryptedGpsMsg+"&device_type=3";
        //httpURL=  "http://track24.unit1.overwatch/track24/service/spi/post?access_id="+DeviceBoardPassword+"&IMEI="+IMEI+"&hex_data="+encryptedGpsMsg+"&device_type=3";

            try { 
                String C0NNECTION_EXTENSION = checkInternetConnection();
                if(C0NNECTION_EXTENSION==null)
                {
                    Dialog.alert("Check internet connection and try again");
                    return null;
                }
                else
                {

                    httpURL=httpURL+C0NNECTION_EXTENSION+";ConnectionTimeout=120000";
            //Dialog.alert(httpURL); 
            HttpConnection httpConn; 
            httpConn = (HttpConnection) Connector.open(httpURL); 
            httpConn.setRequestMethod(HttpConnection.POST); 
            DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream()); 
            byte[] request_body = httpURL.getBytes(); 
            for (int i = 0; i < request_body.length; i++) { 
            _outStream.writeByte(request_body[i]); 
            } 
            DataInputStream _inputStream = new DataInputStream( 
            httpConn.openInputStream()); 
            StringBuffer _responseMessage = new StringBuffer(); 
            int ch; 
            while ((ch = _inputStream.read()) != -1) { 
            _responseMessage.append((char) ch); 
            } 
            String res = (_responseMessage.toString()); 
            responce = res.trim(); 


            httpConn.close(); 
                }
            }catch (Exception e) { 
            //Dialog.alert("Connection Time out"); 
            } 

            return responce;
            } 

My Question: The App freezes whenever the method is called, i.e. whenever the timer has to execute and send data to the web service the App freezes - at times for a few seconds and at times for a considerable amount of time applying to the user as if the handset has hanged. Can this be solved? Kindly help!!

Answers


You are running your networking operation on the Event Thread - i.e. the same Thread that processes your application's Ui interactions. Networking is a blocking operation so effectively this is stopping your UI operation. Doing this on the Event Thread is not recommended and to be honest, I'm surprised it is not causing your application to be terminated, as this is often what the OS will do, if it thinks the application has blocked the Event Thread.

The way to solve this is start your network processing using a separate Thread. This is generally the easy part, the difficult part is

  1. blocking the User from doing anything else while waiting for the response (assuming you need to do this)
  2. updating the User Interface with the results of your networking processing

I think the second of these issues are discussed in this Thread: adding-field-from-a-nonui-thread-throws-exception-in-blackberry

Since it appears you are trying to do this update at regular intervals in the background, I don't think the first is an issue, - for completeness, you can search SO for answers including this one: blackberry-please-wait-screen-with-time-out

There is more information regarding the Event Thread here:

Event Thread


Need Your Help

FBAudienceNetwork v4.1.0 Build Error

ios objective-c xcode facebook

I tried to include FBAudienceNetwork.framework because i want to display banner/interstitial ad unit in my app. But my app gave me several errors.