Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
hims_3009
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

I am creating Http Connection in Thread class and if I call Thread means Thread.start() its working fine.

But If I call it from TimerTask Then Then ConnectionDescriptor returns null or It not get response from Connection.

I also tried with applying

synchronized(Application.getApplication().getEventLock())

{

ConnectionManager connManager=new ConnectionManager()

connManager.start();

}

But though application is not able to create HttpConnection.

Below is a code for Connection which extends Thread

int[] preferredTransportTypes = {TransportInfo.TRANSPORT_TCP_WIFI,TransportInfo.TRANSPORT_BIS_B,TransportInfo.TRANSPORT_WAP2,TransportInfo.TRANSPORT_WAP,TransportInfo.TRANSPORT_TCP_CELLULAR,TransportInfo.TRANSPORT_MDS};
            // Create ConnectionFactory
            ConnectionFactory factory = new ConnectionFactory();
           
//            SelfLearning.getAvailableTransportInfo();
            // Configure the factory
            factory.setPreferredTransportTypes( preferredTransportTypes );
            factory.setAttemptsLimit(1); 
            // use the factory to get a connection
            ConnectionDescriptor conDescriptor = factory.getConnection(url);
           
            System.out.println("Service:- "+service+"\nConnectionDescriptor:-"+conDescriptor);
            if ( conDescriptor != null )
            {
                System.out.println("\n Final URL: "+url);
                System.out.println("\n Row Data: "+rawData);
                           
                //using the connection
                HttpConnection  httpCon = (HttpConnection) conDescriptor.getConnection();
                httpCon.setRequestMethod(HttpConnection.POST);
                httpCon.setRequestProperty("Content-Type", type );
                httpCon.setRequestProperty("Content-Length",""+rawData.length());
                               
                //Now you open up an output stream to write to the connection
                OutputStream os = httpCon.openOutputStream();
                os.write(rawData.getBytes());
                os.flush();
               
                int response=httpCon.getResponseCode();
                if(response==HttpConnection.HTTP_OK)
                {
                    InputStream is=httpCon.openInputStream();
                    if(is != null)
                    {
                        int i;
                        result="";
                        while((i=is.read())!=-1)
                        {
                            result+=(char)i;
                        }
                        System.out.println("Result from Server:- "+result);
                        is.close();
                        onDataEnd();
                    }                           
                }                       
                else
                {
                }
                httpCon.close();
                os.close();
            }

 

 

Lathiya Himanshu
Please use plain text.
Developer
rajeshkparmar
Posts: 104
Registered: ‎08-03-2010
My Device: Blackberry Z10
My Carrier: Idea

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

hi,
why are you used the connection class in thread class?
it will block the connection class process..
try with invoke later
call your connection method in invoke later and don't extend the ConnectionManager class with thread

try this , i think it will solve your problem..

Please mark posts as solved if you found a solution.
Feel free to press the kudos button to thank the user that helped you
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

[ Edited ]

You should put the connection code in the run method of your Thread, not in the constructor.  I would change this in preference to doing what the other poster suggested.

Please use plain text.
Trusted Contributor
hims_3009
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

Hi Peter,

Connection  code is already there in run method not in Constructor If I call is without Timer its working fine but As I am using Timer for Connection I am getting connectionDescriptor-null most of the Time....

 

Lathiya Himanshu
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

It is not clear to me from your post where your connection code is and the other Poster was suggesting putting it in the Constructor, which I was just advising against. 

 

I do not understand this:

"But If I call it from TimerTask Then Then ConnectionDescriptor returns null"

Can you show us how you invoke this code from your TimerTask?  Remember that you should not block a TimerTask. 

 

Also the following is not necessary:

"synchronized(Application.getApplication().getEventLock())

{

ConnectionManager connManager=new ConnectionManager()

connManager.start();

}"

You are blocking the Event Thread just to start a separate Thread, which is not actually related.  So in fact this does not do anything useful. 

 

Please use plain text.
Trusted Contributor
hims_3009
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

[ Edited ]

Here is my Method to invoke the connection code

I removed the EventLock code,

TimerTask configTimerTask=new TimerTask()
            {
                public void run()
                {
//                    synchronized (Application.getEventLock())
//                    {
                            ConnectionData connectionData=new ConnectionData();
                            connectionData.setUrl(ConstantData.CONFIGURATION_AUTH_URL); //getJSONValues();
                            connectionData.setData(sendToJson.toJSON_AUTHENTICATION(false));
                            ConnectionManager connectionManager=new ConnectionManager(connectionData,DelegateBackground.this,JSONConstants.SERVICE_CONFIG_AUTH);
                            connectionManager.start();   
//                    }
                }
            };

 

and ConnectionManager class is attached here

 

 

about that :

"But If I call it from TimerTask Then Then ConnectionDescriptor returns null"

When I am calling  above code for Connection from Timer Its sometimes blocking(means not getting response) or The ConnectionDescriptor i am creating giving "null" value in Connection manager means connection was not created.

Sometimes it is creating connection.

 

So I need to do any changes in code? so that I can run smoothly application and able to create connection



 

 

Lathiya Himanshu
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

From this code it appears you are not blocking the TimerTask.

 

BTW in this context, blocking means any operation that will cause it to wait.  So a network operation is blocking even if the data does come back, because there is a period while you are waiting for the response. 

 

Looking at this code, it seems that I have been confused by this statement:

"When I am calling  above code for Connection from Timer Its sometimes blocking"

 

I think it would be more accurate to say

"Sometimes when I start my network processing from a Timer, it does not get a connection"

 

In fact given that the code is running in a separate Thread, it is irrelevant how it was started.  You could have started the network Thread form some action in your program, or from a Timer or from any other action.  So the issue here is not related to the Timer.

 

So if we rephrase the question as

"Sometimes when I start my network processing, it does not get a connection"

 

And this unfortunately, is what happens .  Things happen that will mean no connectin is available, for example when the user takes a call or is in a lift, or in some area without network cover or turns the network off, like on a plane.  Network connections that are started will fail sometimes to when these events happen too.  You have to be able to cope with it.  At least that is what I think. 

 

Does this answer the question? 

Please use plain text.
Trusted Contributor
hims_3009
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

Peter ,

Yes you can reohrase the question as

"Sometimes when I start my network processing, it does not get a connection"



If we are using it "without Timer" then though we are using connection in Thread then network operation is not blocking.

but "with Timer network operation is someTimes blocking" As what I feel... 

 

If I am using connection in InvokeLater() instead of Thread as suggested by Rajesh then there is a less time its blocking the Thread..

 

So I will use InvokeLater approach instead of Thread..

Thanks Peter..




Lathiya Himanshu
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in HTTP Connection Thread if called from InvokeLater(new Runnable())

Can we define what we mean by blocking here.  In BB land, blocking means that there is a wait involved.  In this context I think you mean it stops processing.  IS that what you mean?  If not, then what do you mean. 

 

I'm going to assume you mean it stops. 

 

Now connection processing do not just stop.  They may appear to stop, but if you have coded the appropriate logging and try/catch processing, you will usually find that the code throws an Exception.  I don't see any of that in your code but since this sort of code must have them, I resumed that you have these elsewhere.  I would make sure that you do and make sure that you are reporting the errors.  Basically network processing completes or throws an Exception, if you tell me you see neither then I will say you just haven't found the Exception yet.   

 

I see nothing different in the way the code is initiated, and the code is the same, if the Timer initiated processing is stopping, then you have a timing issue, not a coding issue.  It does not appear that your Timer code checks to make sure a Thread is not already running, so perhaps your problem is that you start multiple Threads all trying to do the same thing and these are locking out for some reason, or perhaps you hit the "Too Many Threads" exception?

 

Re "If I am using connection in InvokeLater() instead of Thread " if you were actually doing your connection in an invokeLater it would fail as this is a blocking operation and you can't run Blocking (BB definition here) processing on the Event Thread.  

 

If on the other hand, you are just using invokeLater to start the Thread, then its use is irrelevant, it has no impact on the processing, other than delaying its start a little while it waits for the Event Thread. 

 

Short answer, it would surprise if this suggestion actually helped.  But it is your code, your problem and you should do whatever you think might help. 

Please use plain text.