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
Regular Contributor
Posts: 95
Registered: ‎07-16-2010
My Device: 8520
My Carrier: bsnl
Accepted Solution

Error while Reading Inputstream

        Error while Reading Inputstream
        
        Error =>Exception net.rim.device.api.io.ConnectionClosedException: Stream closed
        here is my code modual
        
     

  try
        {
            String url="http://maps.googleapis.com/maps/api/geocode/xml?latlng=21.175128,72.867236&sensor=false";
            String rawData =connectionData.getData();

            httpConn = (HttpConnection) Connector.open(url,Connector.READ_WRITE);
            int status = httpConn.getResponseCode();
            if(status==HttpConnection.HTTP_OK)
            {
                InputStream is=httpConn.openInputStream();
                if(is != null)
                {
                    byte[] data = IOUtilities.streamToBytes(is);
                    result = new String(data, "UTF-8");
                    System.out.println("Result from Server:- "+result);
                    is.close();
                    httpConn.close();
                    onDataEnd();
                }
            }
            //Close the connection.
            if(httpConn!=null)
                httpConn.close();    
        }
        catch (Exception e)
        {
            System.out.println("Exception "+e.toString());
        }

 


        
        
        as upper code i get that error on "byte[] data = IOUtilities.streamToBytes(is);"
        also i try other way to read stream but same error on that webservices call while for the same other service work fine
        
        and one unexpected things is that when i put breackpoint on "byte[] data = IOUtilities.streamToBytes(is);"
        its read perfactly and gives currect responce

 

and when i put breakpoint on next line its not reach ther and give me error
        
        please help...!

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Error while Reading Inputstream

Code looks fine to me, but what is this statement doing?

 

String rawData =connectionData.getData();

Regular Contributor
Posts: 95
Registered: ‎07-16-2010
My Device: 8520
My Carrier: bsnl

Re: Error while Reading Inputstream

Thnks for replay peter

 

as u say code also fine bur its not work for this map.google services

 

and a line "String rawData =connectionData.getData();"  i use when i pass any extra parameter

 

but in current code its not affect else where sorry i forgot to remove that

 

and peter as i mentioon when i wait with breck point at "byte[] data = IOUtilities.streamToBytes(is);"

and after i go to the next step its work so its streange for me any idea according to that

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Error while Reading Inputstream

No, that makes no sense at all.

 

Do you see this on the Simulator or only on device?

 

If on Simulator, can you put a

System.out.println("Just before read");

before the

IOUtilities.streamToBytes(is);

and a

System.out.println("read bytes: " + data.length());

just after.  Also put a System.out.println statement in your Exception catching.

 

Tell us what happens with out a break point - in fact you should be able to copy the debug output for us.

 

That might give us more information. 

Regular Contributor
Posts: 95
Registered: ‎07-16-2010
My Device: 8520
My Carrier: bsnl

Re: Error while Reading Inputstream

thanks you so much to help me on that thread

 

yes peter i things on simmulater

 

i changes as u mention and result as below

 

			int status = httpConn.getResponseCode();
			System.out.println("Status Of "+httpConn.getLength()+" Connection:- "+status);
			if(status==HttpConnection.HTTP_OK)
			{
				InputStream is=httpConn.openInputStream();
				if(is != null)
				{
					result="";
					System.out.println("Just before read");
					
					byte[] data = null;
					try 
					{
						data = IOUtilities.streamToBytes(is);
						System.out.println("read bytes: " + data.length);	
					} 
					catch (Exception e) 
						System.out.println("Exceptio while read bytes: " + e.toString());	
					
					result = new String(data, "UTF-8");
					System.out.println("Result from Server:- "+result);
					is.close();
					httpConn.close();
					onDataEnd();
				}
			}

 

 

and debug out put is :-

 

Status Of -1 Connection:- 200
Just before read
Exceptio while read bytes: net.rim.device.api.io.ConnectionClosedException: Stream closed
Authentication Exception java.lang.NullPointerException

 

---------------------------------------------------------------------------------------------------------

 

i call that web services call on evert 2000 ms so it create any issue ?

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Error while Reading Inputstream

Interesting.  Can you tell us the complete URL you are using, including the connection suffix?  Think I might try this for myself.

Regular Contributor
Posts: 95
Registered: ‎07-16-2010
My Device: 8520
My Carrier: bsnl

Re: Error while Reading Inputstream

hello peter thanks for support

 

URL: http://maps.googleapis.com/maps/api/geocode/xml?latlng=43.4631,-80.5207&sensor=false

in simmulater Suffix : deviceside=true;apn=blackberry.net

 

and to get Suffix i use below code  :

 

	public static String CheckConnection() 
	{
		String ConnectionString="";
		HttpConnection hc=null;
		try 
		{
			//Wifi Connection
			if ( (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) && RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) 
			{ 
				ConnectionString=";interface=wifi";                 
				return ConnectionString;
			}
			//for BES or MDS Connection
			if(CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS))
			{
				boolean connectionFlag=false;
				String post_url;

				//for BES Connections
				post_url="";
				try 
				{
					hc = (HttpConnection) Connector.open("http://www.rim.com"+post_url,Connector.READ_WRITE);
					if(hc.getResponseCode()==HttpConnection.HTTP_OK)
					{
						connectionFlag=true;
						ConnectionString=post_url;
						return ConnectionString;
					}
					if(hc!=null)
						hc.close();
				}
				catch (Exception e) 
				{
					System.out.println(e.toString());
					connectionFlag=false;
				}
				//for MDS Connection
				if(!connectionFlag)
				{
					try 
					{
						post_url = ";deviceside=false";
						hc = (HttpConnection) Connector.open("http://www.rim.com"+post_url);
						if(hc.getResponseCode()==HttpConnection.HTTP_OK)
						{
							ConnectionString=post_url;
							return ConnectionString;
						}  
						if(hc!=null)
							hc.close();
					}
					catch (Exception e) 
					{
						System.out.println(e.toString());
					}
				}
			}
			//for BIS Connection
			if(CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B))
			{
				//BIS Connection
				String post_url = ";deviceside=false;ConnectionType=mds-p**lic";
				try 
				{
					hc = (HttpConnection) Connector.open("http://www.rim.com"+post_url);//Connector.READ_WRITE
					if(hc.getResponseCode()==HttpConnection.HTTP_OK)
					{       
						ConnectionString=post_url;
						return ConnectionString;
					}  
					if(hc!=null)
						hc.close();
				}
				catch (Exception e) 
				{
					System.out.println(e.toString());
				}
			}
			//for WAP Connection
			if(CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT))
			{				//for WAP Connection
				String post_url = null;
				ServiceBook sb = ServiceBook.getSB();
				ServiceRecord[] records = sb.findRecordsByCid("WPTCP"); 
				String uid = null; 
				boolean connectionFlag=false;
				for(int i=0; i < records.length; i++) 
				{
					if (records[i].isValid() && !records[i].isDisabled()) 
					{
						if (records[i].getUid() != null && records[i].getUid().length() != 0) 
						{
							if ((records[i].getUid().toLowerCase().indexOf("wifi") == -1) &&
									(records[i].getUid().toLowerCase().indexOf("mms") == -1))
							{
								uid = records[i].getUid(); 
								break;
							}
						} 
					}
				}
				if (uid != null) 
				{
					post_url= ";deviceside=true;ConnectionUID=" + uid;
				}
				try 
				{
					hc = (HttpConnection) Connector.open("http://www.rim.com"+post_url);
					if(hc.getResponseCode()==HttpConnection.HTTP_OK)
					{
						connectionFlag=true;
						ConnectionString=post_url;
						return ConnectionString;
					} 
					if(hc!=null)
						hc.close();
				}
				catch (Exception e) 
				{               System.out.println(e.toString());
				connectionFlag=false;
				}
				if(!connectionFlag)
				{
					post_url=";deviceside=true;apn=blackberry.net";
					try 
					{
						hc = (HttpConnection) Connector.open("http://www.rim.com"+post_url);
						if(hc.getResponseCode()==HttpConnection.HTTP_OK)
						{
							ConnectionString=post_url;
							return ConnectionString;
						}  
						if(hc!=null)
							hc.close();
					}
					catch (Exception e) 
					{
						System.out.println(e.toString());
					}
				}
			}
		}
		catch (Exception e) 
		{

			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(hc!=null)
					hc.close();
			} catch (IOException e) {
				System.out.println(e.toString());
				e.printStackTrace();
			}
		}
		return "";
	}

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Error while Reading Inputstream

I appreciate the reasons behind doing the connection check that you are doing, but the code you have will cause

 

a) significant problems when you repeat the test, as you do not close the connections

b) delay the processing while you  connect to RIM, so every connection that you do will take twice as long.

 

Before we look further at your code I encourage you to remove this method completely and look instead at using ConnectionFactory to get your connection.  It will do pretty much the same job as your code is doing, without the overhead. 

Regular Contributor
Posts: 95
Registered: ‎07-16-2010
My Device: 8520
My Carrier: bsnl

Re: Error while Reading Inputstream

hii peter,

 

thanks again to replay my post

 

after i remove my code instead i use HttpconnectionFactory

 

and now things are working

 

and i think so as optation a) is my fault

 

and yes i use this code insted of httpconnectionfactory cause on local network connection (edge)  its create probem in some country(india and dubai actualy as my experiance)  on other BIS , BES and WIFI all things Ok

 

and Thanks once again