02-01-2012 03:21 AM
Hy everybody,,,,
i have a connection with http connection on thread...
i want to set specified time to time out, for example i want to force thread and show dialog timeOut information if connection more than 10000ms... so user not wait too long... any body help me?
02-01-2012 03:55 AM
02-01-2012 04:20 AM
If you use ConnectionFactory, you can set a connection timeout on that. I have not actually attempted this nor do I know what happens when the time is exceeded, sorry,
Another option is to start your own Timer, and then try to cancel the connection if the Timer returns before the connection is complete. In this circumstance you can allow the stalling Dialog to continue so the user does not wait and ignore the results of the request if it does eventually come back. Not exactly stopping it but the effect from a user perspective is the same.
02-01-2012 12:55 PM
my part connection like this,
connection = (HttpConnection) Connector.open("http://ecc.ft.ugm.ac.id/?r=blackberryapp/vacancyby comp&offset="+offset+connParameters);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty(net.rim.device.api.i o.http.HttpProtocolConstants.HEADER_CONTENT_TYPE," application /x-www-form-urlencoded");
os = connection.openOutputStream();
if (connection.getResponseCode() == javax.microedition.io.HttpConnection.HTTP_OK) {
is=connection.openInputStream();
String xml = "";
int ch;
while((ch=is.read()) != -1){
sb.append((char)ch);
}
xml = sb.toString();
any the best way to set time out on httpconnection...
02-02-2012 03:13 AM
02-02-2012 03:53 AM
ConnectionFactory doesnt support setting timeout(BlackBerry API 5.0.0 ) tough it has a default timeout. Custom made timer can be a good idea as peter said.
02-02-2012 04:07 AM
02-02-2012 04:28 AM
Ok im have try connection whit connectionFactory... like this
MyConnectionFactory connect = new MyConnectionFactory();
connect.setConnectionTimeout(2000);
connect.setTimeLimit(2000);
ConnectionDescriptor conDesc = null;
try{
conDesc = connect.getConnection("http://192.168.1.26/eccx/?r=blackberryapp/moreinde x&id_comp="+x+"&offset="+offset);
}catch(Exception e){
System.out.println(e.toString()+":"+e.getMessage() );
}
//String response = "";
if(conDesc!=null){
try{
connection = (HttpConnection)conDesc.getConnection();
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("Connection", "close"); // close the connection after success sending request and receiving response from the server
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os = connection.openOutputStream();
// os.write(postBytes);
os.flush();
os.close();
if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
is=connection.openInputStream();
String xml = "";
int ch;
while((ch=is.read()) != -1){
sb.append((char)ch);
}
xml = sb.toString();
jso = new JSONObject(xml);
}
}catch(Exception e){
// System.out.println(e.toString()+":"+e.getMessage() );
Dialog.alert("error, time-out");
}code above is to try if the connection timeout, how to catch the error? and show dialog alert, where i must put the code?