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
New Developer
stephenalistoun
Posts: 17
Registered: ‎07-16-2012
My Device: None
My Carrier: None

Blackberry device cant connect to web service but can with simulator

Hi all,

 

I can connect to webservice using the simulator(using Research In Motion in background) but cannot via actual Black Berry device.

 

How do i go about connecting to web service using a Blackberry device.

 

Code:

 

try {
			int length = bodytext.getBytes().length;
			String contentlength = Integer.toString(length); 
			HttpConnection connection = (HttpConnection) Connector.open(webserviceurl,Connector.READ_WRITE,true);
			
			connection.setRequestProperty("SOAPAction", soapAction);
			connection.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");
    		connection.setRequestProperty("Content-Length",contentlength);
    		connection.setRequestProperty("User-Agent", "kSOAP/2.0");
    		connection.setRequestMethod(HttpConnection.POST);
    		OutputStream os = connection.openOutputStream();
    		os.write(bodytext.getBytes(), 0, bodytext.getBytes().length);
    		os.close();
    		bodytext = null;
    		InputStream is = connection.openInputStream();
    		XmlPullParser xp = new KXmlParser();
    		xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    		xp.setInput (is, null);
    		envelope.parse(xp);
    		obj= (SoapObject) envelope.getResponse();
		} catch (IOException e) {
			System.out.println(e);
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			System.out.println(e);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e) {
			System.out.println(e);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

 

Please use plain text.
Developer
simon_hain
Posts: 15,937
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Blackberry device cant connect to web service but can with simulator

do you use any kind of connection suffix on your webservice url?
maybe http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/transport/ConnectionFactory... can help you out.
----------------------------------------------------------
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
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Blackberry device cant connect to web service but can with simulator

Agree with SImon.

 

To understand the connection suffix and the various connection paths that the BlackBerry can use, I recommend reviewing the following:

 

http://supportforums.blackberry.com/t5/Java-Development/Networking-Transports-II/ta-p/446742

 

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

Please use plain text.
New Developer
stephenalistoun
Posts: 17
Registered: ‎07-16-2012
My Device: None
My Carrier: None

Re: Blackberry device cant connect to web service but can with simulator

Hi Simon and Peter,

 

I have added a suffix to the url but still unable to connect to web service as connection fails

 

The phone picks up "Carrier coverage" and has "carrierUid". I also get a java.io.IOException error if this means anything

 

Code

Connection String

private static String getConnectionString()
    {
        // This code is based on the connection code developed by Mike Nelson of AccelGolf.
        // http://blog.accelgolf.com/2009/05/22/blackberry-cross-carrier-and-cross-network-http-connection
        String connectionString = null;
        System.out.println("Connection String:");
        // Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
        if(DeviceInfo.isSimulator())
        {
        	System.out.println("Device is a simulator and USE_MDS_IN_SIMULATOR is false");
            connectionString = ";deviceside=false";   
        }

        // Wifi is the preferred transmission method
        else if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
        {
        	System.out.println("Device is connected via Wifi.");
            connectionString = ";interface=wifi";
        }

        // Is the carrier network the only way to connect?
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
        {
        	System.out.println("Carrier coverage.");

            String carrierUid = getCarrierBIBSUid();
            if(carrierUid == null)
            {
                // Has carrier coverage, but not BIBS.  So use the carrier's TCP network
            	System.out.println("No Uid");
                connectionString = ";deviceside=true";
            }
            else
            {
                // otherwise, use the Uid to construct a valid carrier BIBS request
            	System.out.println("uid is: " + carrierUid);
                connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mdspublic";
            }
        }

        // Check for an MDS connection instead (BlackBerry Enterprise Server)
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
        {
        	System.out.println("MDS coverage found");
            connectionString = ";deviceside=false";
        }

        // If there is no connection available abort to avoid bugging the user unnecssarily.
        else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
        {
        	System.out.println("There is no available connection.");
        }

        // In theory, all bases are covered so this shouldn't be reachable.
        else
        {
        	System.out.println("no other options found, assuming device.");
            connectionString = ";deviceside=true";
        }

        return connectionString;
    }

 

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

Re: Blackberry device cant connect to web service but can with simulator

This is not correct:

connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mdspublic";

 

Review this KB article to see what you should be specifying:

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

 

Also I recommend that you put MDS first - if you are being used on a corporate phone this is the best connection method - it will automatically swap to WiFi if WiFi is available. 

Please use plain text.
New Developer
stephenalistoun
Posts: 17
Registered: ‎07-16-2012
My Device: None
My Carrier: None

Re: Blackberry device cant connect to web service but can with simulator

Hi Peter,

 

I changed the connection string as follows: connectionString = ";deviceside=false;ConnectionUID="+carrierUid + "";

 

Is that correct because just get a error saying "IOException: Stream not in setup state".

 

Stephen

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

Re: Blackberry device cant connect to web service but can with simulator

I thought this was the WAP 2 connection method in which case the connection suffix is:

";deviceside=true;ConnectionUID=" + uid

 

not deviceside=false;....

Please use plain text.