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
Trusted Contributor
Posts: 115
Registered: ‎11-16-2011
My Device: Smartphone Storm 9900
My Carrier: 3

Re: set timer task on httpconnection?

[ Edited ]
final int responseCode = connection.getResponseCode();
timerTask = new TimerTask() {
	  	public void run() {
						try {
							 if (responseCode == 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); 
		          }
				connection.close();
				Dialog.alert("Maaf Koneksi error");
			} catch (Exception e) {
				// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}			
		};

connectionTimer = new Timer();
connectionTimer.schedule(timerTask, 2000);
}catch(Exception e){
    System.out.println(e.toString()+":"+e.getMessage());
		    }
			
		}
    UiApplication.getUiApplication().invokeLater( new Runnable() {                
        public void run() {                    
            ScreenVacDetail screenCompany = new ScreenVacDetail(jso,offset);
            UiApplication.getUiApplication().pushScreen(screenCompany);
        }
    });
    }
};

 Thats true? but im nothing get event... where is the problem? thanks again

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

Re: set timer task on httpconnection?

Sorry that looks nothing like what I suggested (at least what I thought I had).  And I would ignore the comment made by enamur_rashid  as that makes no sense to me either.

 

Remember the pseudo code I gave you was this:

<schedule Timer here>

int responseCode = connection.getResponseCode();

<cancel Timer here>

 

So you must schedule the Timer before you do the connection.getResponseCode(), because it is that statement which (in most cases anyway) actually blocks.

 

You cancel the Timer immediately after this statement, because, if you get past the statement, then you are not being blocked.

 

OK?

 

If I could make one other random comment.  You seem to have cut/pasted various bits of code in various places.  Some of what you have done makes no sense tome.  I suggest before you make code changes like this you make sure that it makes sense.  Don't do something just because I or anyone else tells you to.  Unless you understand why you are coding something, don't code it and ask.  Just a random observation, may not even be applicable....

Trusted Contributor
Posts: 115
Registered: ‎11-16-2011
My Device: Smartphone Storm 9900
My Carrier: 3

Re: set timer task on httpconnection?

how about your say, cancel if the connection Ok? im confuse....

pliease can u view simple code?

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

Re: set timer task on httpconnection?

[ Edited ]

Rather than giving you code, I think it is much better for you to review the API and understand what you are doing.  Cut/pasting code is a useful learning approach, but in this case, I don't think it helps much.

 

In answer to your question - the thing you cancel is the Timer which if left, will close the http connection and thus cause the connection to have an error - which you can detect as a time out.  So, look at Timer API and note the cancel method. 

 

As well as the API, you will find information on Timer and TimerTask by searching the internet, for example:

http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html