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
Contributor
Hope_01
Posts: 13
Registered: ‎08-26-2010
My Device: 8120
Accepted Solution

Connection Timeout Problem....

hii,,

I am developing a application in which there is need of connection timeout..But how we will check the timeout without blackberry MDS service available on that mobile..Means that if that mobile is not using BES services and using Wifi or GPRS for net connection........Please help......:smileysad:

Developer
dineshaggarwal
Posts: 112
Registered: ‎04-28-2010
My Device: 8520

Re: Connection Timeout Problem....

hello,

In blackberry by default connection timeout is ConnectionTimeout=1200000 mili seconds.

 

if u want to customize in ur application place ;ConnectionTimeout=1200000 with ur webpage/data request.

for e.g.

http://www.blackberry.com/developers;ConnectionTimeout=10000

 

If u find that answer helpful, do not forget to click on kudos button.

 

Regards

Developer
vivart
Posts: 147
Registered: ‎08-27-2010
My Device: 9800

Re: Connection Timeout Problem....

if you are using DirectTCP for http connection 

AFAIK this method will not work.

Contributor
Hope_01
Posts: 13
Registered: ‎08-26-2010
My Device: 8120

Re: Connection Timeout Problem....

The problem is coming by using  "http://www.blackberry.com/developers;ConnectionTimeout=10000" is that after calling the webservice 8-10 times it doesn't connect  to the webservice again..And then we have to reinstall the application again.Is there another way to check the time out....I have to check the time out  while calling webservice each time.....

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Connection Timeout Problem....

As far as I know, setting connection timeout via a suffix string is only supported for BES connections.  I believe the official way to do it is to cast you connection to SocketConnectionEnhanced and use setSocketOptionEx.

 

That said, regardless of timeout, calling the webservice 8-10 times should not cause a failure as you have described.  I strongly suspect a bug in your logic, I would guess that you do not close the connection properly.

 

You might find the code in this Thread useful:

http://supportforums.blackberry.com/t5/Java-Development/Sample-HTTP-Connection-code-and-BIS-B-Access...

 

At the very least this will help you add tracing code to your own connection, so that you log the processing properly and so can tell us exactly what problem you are seeing.  "It doesn't connect" is not a very useful problem description, unfortunately.

Contributor
Hope_01
Posts: 13
Registered: ‎08-26-2010
My Device: 8120

Re: Connection Timeout Problem....

Hi Peter_Strange....I have checked the Connection Thread throught the application but the problem is only coming while using ConnectionTime out...As said by Vivart i thik i need solution for the DirectTCP for http connection Timeout...can anyone give me a sample code for that.... thaks in advance...:smileyhappy:

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Connection Timeout Problem....

As noted in my previous post, but with a spelling correction....

"I believe the official way to do it is to cast your connection to SocketConnectionEnhanced and use setSocketOptionEx."

Contributor
Hope_01
Posts: 13
Registered: ‎08-26-2010
My Device: 8120

Re: Connection Timeout Problem....

[ Edited ]

I have tried that also....I am using this code for webservice calling....

import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;


public class ConnectionThread extends Thread {
    boolean start = false;
     boolean stop = false;
    String url;
     String data;
    public boolean sendResult = false;
    public boolean sending = false;
    String requestMode = HttpConnection.POST;
    public String responseContent;
  
    
  
    
    public void run() {
        while (true) {
            if (start == false && stop == false) {
                try {
                    sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (stop) {
                return;
            } else if (start) {
                http();
            }
        }
    }
    int ch;
    private void getResponseContent( HttpConnection conn ) throws IOException {
        InputStream is = null;
        is = conn.openInputStream();
          int len = (int) conn.getLength();
            if ( len > 0 ) {
                int actual = 0;
                int bytesread = 0;
                byte[] data = new byte[len];      
                while ( ( bytesread != len ) && ( actual != -1 ) ) {
                    actual = is.read( data, bytesread, len - bytesread );
                    bytesread += actual;
                }
                responseContent = new String (data);
            } else {
                
                while ( ( ch = is.read() ) != -1 ) {
               }
            }
   }
    
    private void http() {
        System.out.println( url );
        HttpConnection conn = null;
        OutputStream out = null;
        int responseCode;
        try {
            conn = (HttpConnection) Connector.open(url);
            conn.setRequestMethod(requestMode);
            out = conn.openOutputStream();
            out.write(data.getBytes());
            out.flush();
            responseCode = conn.getResponseCode();

            if (responseCode != HttpConnection.HTTP_OK) {
                sendResult = false;
                responseContent = null;
               
               
            } else {
                sendResult = true;
                getResponseContent( conn );
            }
            start = false;
            sending = false;

        } catch (IOException e) {
            
        	start = false;
            sendResult = false;
            sending = false;
          
       	
        } 

    }

    public void get(String url) {
        this.url = url;
        this.data = "";
        requestMode = HttpConnection.GET;
        sendResult = false;
        sending = true;
        start = true;
    }
    
    public void post(String url, String data) {
        this.url = url;
        this.data = data;
        requestMode = HttpConnection.POST;
        sendResult = false;
        sending = true;
        start = true;
    }

    public void stop() {
        stop = true;
    }

}


In 

conn = (HttpConnection) Connector.open(url);

I tried to use that SocketConnection still its creating problem.....Can u tell me where to use it exactly...

 

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Connection Timeout Problem....

Contributor
Hope_01
Posts: 13
Registered: ‎08-26-2010
My Device: 8120

Re: Connection Timeout Problem....

I am using OS version4.5 only... :smileysad: Is there some another way out for this problem.... Plz help...