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 Contributor
Posts: 8
Registered: ‎03-24-2012
My Device: Developer
My Carrier: 9800

Http IO exception

Hello all,

 

I am working on a project  using 5.0 api.I have wriiten Http Connetion code to Get the data.

It works fine,..on simulator always. but on device it is having some strange behaviour,.,,,
Its works fine ,,..bt randomly get IO exception ..and after that no other http request process..untill i reboot the device...

I am adding connection suffix properly ..for different connection..........

Connector.open(url).........gives IO exception.
 ......
here is my code,,, 

 

String url= url.concat(getConnectionString())

HttpConnection httpCon = null;
InputStream iStream = null;
String result="";
try {
httpCon = (HttpConnection)Connector.open(url);
httpCon.setRequestMethod(HttpConnection.GET);
iStream = httpCon.openInputStream();
int httpResponse = httpCon.getResponseCode();
System.out.println("Response code is>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<"+httpResponse);
if(httpResponse != 200)
return null;
System.out.println("Response code is>>>>>>>>>>>>>>>>>"+httpResponse);
callSuccess.setValue(true);
InputStreamReader in = new InputStreamReader(iStream);
StringBuffer sb=new StringBuffer();
char[] ch=new char[1020];
while(in.read(ch)!=-1)
{
sb.append(ch);
ch=new char[1020];
}
result=sb.toString();
System.out.println("Result>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>="+result);

}
catch (IOException ioe)
{
System.out.println("Exception occours here>>>>"+ioe);
ioe.printStackTrace();
}
finally
{
try
{
if(iStream != null)
iStream.close();
if(httpCon != null)
httpCon.close();
}catch (IOException e) {
System.out.println("Exception occours here>>>>");
}
}

return result;

 

 

 

////////////////////

public static String getConnectionString()
{
String connectionString = null;

// Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
if(DeviceInfo.isSimulator())
{
connectionString = ";deviceside=true";
System.out.println("<<<<<<<<<<<<If simulator>>>>>>>>>>>>>>>>");
}


// Device is connected via Wifi
else if( (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) && RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN))
{
connectionString = ";interface=wifi";
System.out.println("<<<<<<<<<<<<If Wifi>>>>>>>>>>>>>>>>");

}
// Is the carrier network the only way to connect?
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
{
String carrierUid = getCarrierBIBSUid();
if(carrierUid == null)
{
//Has carrier coverage, but not Carrier UId So use the carrier's TCP network with APN settings
connectionString = ";deviceside=true";
System.out.println("<<<<<<<<<<<Direct tcp>>>>>>>>");
}
else
{
// otherwise, use wap 2.0 gateway
connectionString = ";connectionUID="+carrierUid + "";
System.out.println("<<<<<<<<<<<<<<<<<If Wap 2.0>>>>>>>>>>");
}

}

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

}
//Check for Blackberry internet service
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_BIS_B) == CoverageInfo.COVERAGE_BIS_B){
connectionString = ";deviceside=false;
System.out.println("<<<<<<<<<<<<<<<<<If BIS>>>>>>>>>>");

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

// In theory, all bases are covered so this shouldn't be reachable.

return connectionString;
}

 

 

pls suggest something,,,,,,,

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

Re: Http IO exception

use connectionfactory.
----------------------------------------------------------
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
New Contributor
Posts: 8
Registered: ‎03-24-2012
My Device: Developer
My Carrier: 9800

Re: Http IO exception

tried ,,,,,bt same behaviour....
Smiley Sad 

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

Re: Http IO exception

does the exception have any detail message?
can you post the updated code snippet using the factory?
----------------------------------------------------------
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
Highlighted
New Contributor
Posts: 8
Registered: ‎03-24-2012
My Device: Developer
My Carrier: 9800

Re: Http IO exception

yeah for sure,,,,..

 

public StringBuffer doGet(String url) throws Exception {
HttpConnection conn = null;
StringBuffer buffer = new StringBuffer();

try {
if ((url == null) || url.equalsIgnoreCase("") || (cf == null)) {
return null;
}
ConnectionDescriptor connd = cf.getConnection(url);
String transportTypeName = TransportInfo.getTransportTypeName(connd.getTransportDescriptor().getTransportType());
conn = (HttpConnection) connd.getConnection();

log.info("HTTP-GET (" + transportTypeName + "): " + conn.getURL());
int resCode = conn.getResponseCode();
String resMessage = conn.getResponseMessage();
log.info("HTTP-GET Response: " + resCode + " " + resMessage);

switch (resCode) {

case HttpConnection.HTTP_OK: {
InputStream inputStream = conn.openInputStream();
int c;

while ((c = inputStream.read()) != -1) {
buffer.append((char) c);
}

inputStream.close();
break;
}

case HttpConnection.HTTP_BAD_REQUEST: {
InputStream inputStream = conn.openInputStream();
int c;

while ((c = inputStream.read()) != -1) {
buffer.append((char) c);
}

inputStream.close();
break;
}

case HttpConnection.HTTP_TEMP_REDIRECT:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_MOVED_PERM: {
url = conn.getHeaderField("Location");
buffer = doGet(url);
break;
}

default:
break;
}
log.info("HTTP-GET Body: " + conn.getType() + "(" + buffer.length() + ")");
if ((conn.getType() != null) && conn.getType().trim().startsWith("text/javascript")) {
log.debug(buffer.toString());
}

} catch (Exception e) {
throw e;
} finally {
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}

return buffer;
}

 

this code is also woking bt generates exception randomly.....
and there is no detail msg...while i debug on device...except,,,IO Exception..