Thales' cellular IoT products business is now part of Telit Cinterion, find out more.

You are here

J2me - OutofMemory in HTTP request | Telit Cinterion IoT Developer Community

December 9, 2016 - 10:34am, 2525 views

I have a method in my BGS5T project in which, after 2 days of normal working, it collapses. The error presented is the following

Contacting website...

java.lang.OutOfMemoryError

       (stack trace incomplete)

The said method is the one used to communicate with a website. It receives a string and a mode selector (i=1 or smth else) and procedes with the request. Here's the code:

 
     void enviarPost(int i, String comando)throws IOException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{     
		  System.out.println("Contacting website...");
		    if(i == 1)
		    	{
		        url = "http://websitedummy.com.pl/index.php?IMEI=" + imeiX + "&IP=" + ipX;
		    	}
		    	//53543D303B44723D4E616F
		    	else
		    	{
		    	url2 = comando;		    	
		    	url = "http://websitedummy.com.pl/index.php?data={\"IMEI\":\""+imeiX+"\",\"TS\":\"20/04/13-08:31:44\",\"SER\":\""+url2+"\"}";		    	 
		    	
		    	}
		    	
		        

		        try {
		        	Thread.sleep(1000);
		            connection = (HttpConnection) Connector.open(url);
		        	Thread.sleep(500);
		            connection.setRequestMethod(HttpConnection.GET);
		        	Thread.sleep(500);
		          connection.setRequestProperty("Content-Type", "text/plain");

		        	Thread.sleep(500);
		        	int con = 0;
		        	try{
		        	 con = connection.getResponseCode();
		        	} catch (Exception e4)
		        	{ 
		        		System.out.println(e4);
		        		
		        	}		        	
		            if (con == HttpConnection.HTTP_OK) {
		            	System.out.println("Vamos");
		                inputstream_ = connection.openInputStream();
		                int ch;
		                while ((ch = inputstream_.read()) != -1 ) {
		                    dataReceived.append((char) ch);
		                    	
		                }
		                    
		                System.out.println("Atualizado.");
						
		                acabouatualizar=1;
		              
		                inputstream_.close();
		                connection.close();
		            } else {
		            	System.out.println("Error");
		                // Connection not ok
		            }
		        } catch (Exception e) {
		          System.out.println("EXCEÇÂO 1 - " + e);
		          
		        } finally {
		            if (inputstream_ != null) {
		                try {
		                	
		                    inputstream_.close();
		  
		                } catch (Exception e1) {
		                	System.out.println("EXCEÇÂO 2- " + e1);
		                }
		            }
		            if (connection == null) {
		                try {
		                	connection.close();
		                    
		                } catch (Exception e2) {
		                	System.out.println("EXCEÇÂO 3- " + e2);
		                }
		            }
		        }
		        }
 

To solve the issue i thought about clearing all the variables used in the connection. The problem is I kind of have to be almost sure what the issue is because the error will take 2 days to happen and this will cost me a great amount of time.

Any suggestions? 

Thanks guys.