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
Highlighted
Regular Contributor
Posts: 73
Registered: ‎07-12-2011
My Device: 9800
My Carrier: Programmer

Network Connection Failed

[ Edited ]

I've implemented timer task on background application.

 

I've collect current lat and long. and send to server each 30 seconds.

 

I've used below code to send the information to server.  It send successfully.. 

 

My problem is , after i've check 10 minutes,  Unable to send . its throws No network. I've checked browser too but no network. 

 

If reset the device, its working again well. But the same problem occure after 5 or 10 mins.

 

How to resolve this?

 

My code is.

new Thread()
{
	public void run()
	{
		
		suffix= NetworkUtils.getConnectionUrl("http://www.google.co.in", NetworkTypes.CONSUMER_NETWORK_PRIORITIES);
		if(suffix==null)
		{
			showDialog("No network..");
		}
		else
		{
								
			try 
			{
				StreamConnection connection = (StreamConnection) Connector.open(url+suffix);
				((HttpConnection) connection).setRequestMethod(HttpConnection.GET);
				int responseCode = ((HttpConnection) connection).getResponseCode();
				if (responseCode != HttpConnection.HTTP_OK) 
				{
					showDialog("Unexpected response code :"+ responseCode);
					connection.close();
					return;
				}
				((HttpConnection) connection).getHeaderField("Content-type");
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				InputStream responseData = connection.openInputStream();
				byte[] buffer = new byte[1000];
				int bytesRead = responseData.read(buffer);
				while (bytesRead > 0) 
				{
					baos.write(buffer, 0, bytesRead);
					bytesRead = responseData.read(buffer);
				}
				baos.close();
				connection.close();
				String s = new String(baos.toByteArray());
				showDialog("Responce from server "+s);
			}
			catch (IOException e) 
			{
			}
		}
	}
}.start();		

 

 

 

 

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Network Connection Failed

i don't see how you send anything with the code above. are you sure that you use a GET connection to post something to the server?

Every 30s is quite a lot. I would suggest to lower that interval and add logging in the exception block.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Network Connection Failed

I agree with Simon.

 

Rather than having a Thread that you start periodically, I suggest that you have a Thread that goes into a loop, transmitting and then waiting.  You need to write this Thread properly so that it completely closes the connections it uses - even when these timeout or have some other error. Then you are only using one connection. 

 

This code I suspect will use multiple connections - one per Thread - and then does not close these correctly.  There are only a limited number of connections available, so I suspect that, after a time, you will use them all and then the device is effectively offline, as you have found. 

 

Now of course you will need to buffer the locations you collect because it might be that the network goes down.  But you still want to keep collecting your location, and send all the historical values in later when you reconnect.  So you might like to re-think this application design to cope with this possibility too.