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
sunaina
Posts: 10
Registered: ‎01-16-2013
My Device: BB7 and BB10
My Carrier: Airtel

How to use Port Number in HttpConnectionFactory

Hi,

 

I am trying to access my webservice through Blackberry application 7.1 os.

 

When i try the URL without port number it works all fine both on the device and simulator both with wifi enabled and Internet service provider enabled:

 

url = http://ipaddress/test/JSON/

 

where in now my webservices is placed in Azure and hence I need to specify a port number.

 

But when I try to access URL with port number it doesn't seem to work on the device where as it works fine on the simulator . I need to make it work with the device Internet service provider. The Strange behaviour here is the same URL works fine with wifi enabled. As soon as I disable wifi and enable Internet service provider it deosn't  seam to work.

 

url = http://ipaddress:89/test/JSON/

 

 

Basicallly I am making use of httpConnectionFactory.

 

HttpConnectionFactory current=new HttpConnectionFactory(url);
try {
c=current.getNextConnection();
} catch (NoMoreTransportsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);

c.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: " + rc);
}

is = c.openInputStream();

 

int len = (int)c.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;
}

//Get the JSON String
crossingJSONString = new String(data);

}
else
{
int ch;
while ((ch = is.read()) != -1)
{
//TODO
/*
process((byte)ch);
*/
}
}
}
catch (ClassCastException e)
{
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
if (is != null)
is.close();
if (c != null)
c.close();

}

 

 

 

I am totally confused why it doesn't work when I add the port number.

 

Do I need to specify the port number in httpconnectionfactory? Is there at all any specific way to do this?

 

Is there a possibility that device is blocking upto 1024 ports?

 

Somebody please help me out...

 

Thanks in advance,

Sunaina

Please use plain text.
Developer
simon_hain
Posts: 15,791
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: How to use Port Number in HttpConnectionFactory

the device does not block any ports, and you seem to use the port correctly.
is the ip address actually reachable via the provider? or is it maybe a local address?
it could be possible that the provider blocks certain ports, but you would have to clear that with their support.
i'd suggest to check if it works with port 80 (default http) to ensure that the code works correctly.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.