09-07-2012 04:35 AM
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",con tentlength);
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_NAMESP ACES, 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();
}
09-07-2012 04:44 AM
09-07-2012 04:56 AM
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-Develo
09-10-2012 05:12 AM
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-cr oss-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;
}
09-10-2012 07:55 AM
This is not correct:
connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mdspublic";
Review this KB article to see what you should be specifying:
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.
09-10-2012 12:58 PM
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
09-10-2012 05:00 PM
I thought this was the WAP 2 connection method in which case the connection suffix is:
";deviceside=true;ConnectionUID=" + uid
not deviceside=false;....