• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to know when a HttpURLConnection is on the "connected" state?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm working on a university project to create a J2ME emulator, using J2SE API. Therefore, in order to allow HTTP connections to J2ME applications, I'm using a from the J2SE API. However I'm stucked with it, because I have to check if the instance is in the connected state, but there is no method available to return it. Does anyone knows if there is a way to know that?

Thanks in advance,

Fuad Abinader
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see anything in the JavaDoc to help, but it does say that connect() is ok if it's already connected. If you just wanted to say:

see if it's ok to just always connect.

I suppose you could extend HttpURLConnection and add a method to return the protected "connected" variable, but I don't think I like that much.
 
slicker
Posts: 1108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Http - isn't it ask & receive?

So you ask; and either you get, or you timeout. Right?

I use HttpURLConnection. I set one up at start up and use it over again all day long. Only gives a problem if the remote server that I'm trying to connect to goes down, or was never up.

Why do you need to check on connected()?
 
Fuad Abinader
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Well, the problem is that I'm trying to create a J2ME emulator, and for that, I'm using J2SE API to provide J2ME services in the way the J2ME API specifies. For that, I'm making a wrapper for HTTP, in a way that I use a J2SE HttpURLConnection to simulate the behavior of a J2ME HttpConnection. The problem is that they have different behavior regarding methods that cause the transition to the connected state, and also they behave differently on when to return exceptions.
For example, in J2ME, if I open a HTTP connection, and call connection.getDate(), this causes the connection to go to the connected state; therefore, if I call the method connection.setRequestProperty(), this method should throw an IOException, because the connection will be at the connected state. If I do the same on a instance of J2SE HttpURLConnection, no exception is thrown. Therefore I have to check if the connection is in the connected state in order to return the IOException.
Can anyone help me on this issue? Any hints?

Regards,

Fuad Abinader
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, a much more interesting problem for sure! Just for grins, see if you can "cheat" and use reflection to change the visibility of the protected "connected" boolean. See if it even has the right value at the right time.
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic