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: 92
Registered: ‎03-09-2013
My Device: bold 9800
My Carrier: blackberry

SocketConnectionEnhanced not working

Hello every body and special hello to Peter_Strange Smiley Very Happy,

 

I am trying to make a time out for HttpConnection. Without using SocketConnectionEnhanced  my transactions took almost 2 minutes for getting a response. I have tried SocketConnectionEnhanced for making Time Out, but it is not working even it is delaying my normal transactions upto 5 minutes. I just want to make a timeout for HttpConnection if no Connection is established. I am going to paste sone portion of my Code. Please help me out there..

 

public static String[][] AgAppXMLParser(String parUrl) {

Document doc;
StreamConnection conn;
String _node,_element;
String[][] xmlRespone = null;
try{


String url = AgAppHelperMethods.getUrl() + parUrl;

url = AgAppHelperMethods.getNetworkDependentSuffixedURL(url);

if(url == null)
{
xmlRespone = new String[1][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = "No Connection Available!";
xmlRespone[1][0] = "Invalid server response.Please try again later";
xmlRespone[1][1] = "12";
return xmlRespone;
}

/* conn=(StreamConnection)Connector.open(url,Connector.READ_WRITE,true);*/
ConnectionFactory connect = new ConnectionFactory();


HttpConnection connection = null;//(HttpConnection) Connector.open(url); //

ConnectionDescriptor conDesc = null;
try{
conDesc = connect.getConnection(url);
}catch(Exception e){
xmlRespone = new String[1][2];
xmlRespone[0][0] = "null";
xmlRespone[0][1] = e.getMessage();
return xmlRespone;
}

connection = (HttpConnection) conDesc.getConnection();
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty(net.rim.device.api.io.http.HttpProtocolConstants.HEADER_CONTENT_TYPE,"application /x-www-form-urlencoded");

SocketConnectionEnhanced sce = (SocketConnectionEnhanced) connection;
short sceOption = SocketConnectionEnhanced.READ_TIMEOUT;
long timeoutTime = 3000; // 3 seocnd timeout

 sce.setSocketOptionEx(sceOption, timeoutTime);

 

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();

---Code Stuck there for almost 5 minutes here if I use SocketConnectionEnhanced  methods...-------
doc = docBuilder.parse(connection.openInputStream());
if (connection.getResponseCode() != javax.microedition.io.HttpConnection.HTTP_OK){
xmlRespone = new String[2][2];
xmlRespone[0][0] = String.valueOf(connection.getResponseCode());
xmlRespone[0][1] = String.valueOf(connection.getResponseCode());
xmlRespone[1][0] = String.valueOf(connection.getResponseCode());
xmlRespone[1][1] = String.valueOf(connection.getResponseCode());
return xmlRespone;
}

doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("*");
_node=new String();
_element = new String();
xmlRespone = new String[list.getLength()][2];


for (int i=0;i<list.getLength();i++)
{
Node value=list.item(i).
getChildNodes().item(0);

_node=list.item(i).getNodeName();
if(value==null)
{_element=null;}
else
{_element=value.getNodeValue();}
xmlRespone[i][0] = _node;
xmlRespone[i][1] = _element;

}//end for


}

 

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

Re: SocketConnectionEnhanced not working

Just a quick note to say I have seen this question.  Apologies for delay in getting round to answering and it will be a bit longer for a more detailed response because I don't have much time right now.

 

But I am hoping that this link will provide you with more information:

http://supportforums.blackberry.com/t5/Java-Development/request-time-out-using-HttpConnection/m-p/24...

 

Have a look at this, and the provided links there, and see if you come to some conclusion.

 

Basically SocketConnectionEnhanced timeout does not work everywhere.  You have to implement your own timeout if you want it to work everywhere.  And there are some curve balls when trying to do that.