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
Highlighted
New Contributor
Posts: 5
Registered: ‎04-10-2011
My Device: 9700, 9800
My Carrier: viettel

BB 9700, 9800 sometime my app can not create http connection, other apps can not either.

Hi everybody,

I'm developing a app on BB using java. My app connects server using direct TCP method.

Everything works well except that some time my app cannot create http connection. And from that point, neither my app nor other apps on blackberry such as: foursquare, app world,.. can connect to internet. Only app can connect internet is the WAP browser. Every thing works fine again if I reboot my blackberry. The BB Im using are:BB 9700, 9800.

I've tried many ways to fix the issue such as using Generic Network Framework (BB 4.7 and ealier), new network api for BB 5.0 and later, newnetwork API for BB 6.0 and later, but all don't work.

 

I also carefully checked my APN configuration.

In addition, It's hard to reproduce the issue. Therefore it has taken me alot of time to figure out what happen to my app.

 

Any ideas or helps would be great appreciated.

Thank you

 

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

Re: BB 9700, 9800 sometime my app can not create http connection, other apps can not either.

New Developer
Posts: 41
Registered: ‎09-22-2009
My Device: Not Specified

Re: Sample HTTP Connection code and BIS-B Access

Hi,

 

What is the compatibility of the library ? 

 

poiuytrez

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

Re: Sample HTTP Connection code and BIS-B Access

Not sure what you mean.  It was written using OS 4.5, should work OK on anything later than that.  Does that answer the question?

Developer
Posts: 70
Registered: ‎09-17-2008
My Device: BlackBerry Bold 9000
My Carrier: Rogers

Re: Sample HTTP Connection code and BIS-B Access

Firstly - thank you for this sample! Def. helpful when getting starting with Blackberry dev.

 

Secondly - I noticed this sample code doens't have support for the PUT or DEL command. I skimmed through the code and thought I could add it in the HTTPRequestRunnable class's run() method - but HttpConnection class doesn't support PUT. Do you know how I could add PUT functionality? 

 

Thanks!

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

Re: Sample HTTP Connection code and BIS-B Access

The only way that I am aware that you could add PUT functionality yourself would be to code the connection as a socket connection and write the whole stream yourself from the Http specifications.  But I would ask this as a separate forum post and see if anyone else as other options. 

Developer
Posts: 33
Registered: ‎10-29-2010
My Device: Not Specified

Re: Sample HTTP Connection code and BIS-B Access

why i got error like this when i tried to test this app on my device.

 

Exception Sending request - Correct URL or Connection used.

 

i use the default url [http://www.blackberry.com/

and the only connection that available is BIS-B

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

Re: Sample HTTP Connection code and BIS-B Access

As noted in this Post on this Thread:

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

the supplied code does not support BIS-B.  Access to BIS-B is restricted, you should get it officially as an Alliance member or by requesting push as described in this Thread. 

New Developer
Posts: 5
Registered: ‎04-06-2011
My Device: BlackBerry 9300 Curve
My Carrier: Vodafone Italy

Re: Sample HTTP Connection code and BIS-B Access

Hi All! I am a very new developer on BB, but I do have some huge experience on Windows Mobile 6.x

I have been reading this post back and forth, and I still ask myself  "why is this so complicated" ???

 

Let me introduce my environment:

+ Eclipse 1.3 Helios with the standard "BlackBerry-SDK- 9800" simulator

+ A real device BlackBerry Curve 9300 and (of course) BlackBerry desktop

+ Signing keys installed and working

 

I wanted to start some coding and I created this:

 

*** BEGIN FILE: HTTPENGINE.JAVA


package vortika.symmetry.blackberry;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class HttpEngine extends Thread {
 
 public String Url;
 public String Method; // GET or POST
 private String EngineStatus;
 private String DownloadedData;
 
 
 public HttpEngine(String sUrl, String sMethod)
 {
  this.Url = sUrl;
  this.Method = sMethod;
  this.EngineStatus = "";
  this.DownloadedData = "";
 }
 
 public String GetDownloadedData()
 {
  return DownloadedData;
 }
 
 public String GetEngineStatus()
 {
  return EngineStatus;
 }

 public String DownloadData()
 {
  try
  {
      EngineStatus = "OPENING_CONNECTION";
   HttpConnection connection = (HttpConnection)Connector.open(Url);
   EngineStatus = "SETTING_REQUEST_METHOD";
   connection.setRequestMethod(Method);
   
   EngineStatus = "GETTING_RESPONSE_CODE";
   int responseCode = connection.getResponseCode();
   if (responseCode == HttpConnection.HTTP_OK)
   {
    EngineStatus = "DOWNLOADING_DATA";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream responseData = connection.openInputStream();
    byte[] buffer = new byte[1024];
    int bytesRead = responseData.read(buffer);
    while(bytesRead > 0)
    {
     baos.write(buffer, 0, bytesRead);
     bytesRead = responseData.read(buffer);
    }
    baos.close();
    EngineStatus = "CLOSING_CONNECTION";
    connection.close();
    EngineStatus = "SAVING_DATA";
    DownloadedData = new String(baos.toByteArray());
    EngineStatus = "READY";
   }
   else
   {
    connection.close();  
    EngineStatus = "RESPONSE_CODE_FAILED";
   }
  }
  catch (IOException ex)
  {
   // handling exception
   EngineStatus = "EXCEPTION: " + ex.getMessage();
  }  
  return DownloadedData;
 }
 
 
 public void run()
 {
  try
  {
      EngineStatus = "OPENING_CONNECTION";
   HttpConnection connection = (HttpConnection)Connector.open(Url);
   EngineStatus = "SETTING_REQUEST_METHOD";
   connection.setRequestMethod(Method);
   
   EngineStatus = "GETTING_RESPONSE_CODE";
   int responseCode = connection.getResponseCode();
   if (responseCode == HttpConnection.HTTP_OK)
   {
    EngineStatus = "DOWNLOADING_DATA";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream responseData = connection.openInputStream();
    byte[] buffer = new byte[1024];
    int bytesRead = responseData.read(buffer);
    while(bytesRead > 0)
    {
     baos.write(buffer, 0, bytesRead);
     bytesRead = responseData.read(buffer);
    }
    baos.close();
    EngineStatus = "CLOSING_CONNECTION";
    connection.close();
    EngineStatus = "SAVING_DATA";
    DownloadedData = new String(baos.toByteArray());
    EngineStatus = "READY";
   }
   else
   {
    connection.close();  
    EngineStatus = "RESPONSE_CODE_FAILED";
   }
  }
  catch (IOException ex)
  {
   // handling exception
   EngineStatus = "EXCEPTION: " + ex.getMessage();
  }  
 }
}

 

*** END OF FILE

 

 

As you can see there are two methods:

run() = it was supposed to be the "standard" way of approaching the problem of getting some data from a website

DownloadData() = same as run, but you can use it "directly".

 

run() doesn't work, not in the simulator, not even on the device. It just hangs up, without any error or exceptions. As soon as I call my instance httpEngine.start() the code stops around this point "int responseCode = connection.getResponseCode();"

 

The method DownloadData() works PERFECTLY both on simulator and device.

 

This is how I call it from the Splash Screen of my application:

HttpEngine httpEngine = new HttpEngine("http://www.vortika.com/ipaddress.asp", "GET");
String sOutput = "BlackBerry Network IP: " + httpEngine.DownloadData();

 

Where is the problem then? My problem is that I am scared as hell after reading all the posts on the forums about people having huge issues with http connections. Have I been so lucky that I just made it work from the very first time?!?!  If the answer is YES, I will be even more scared !!!

 

Now please, I will be very grateful if someone (peter? wow.... that would be great!) explains me why I should get my code EXTREMELY complicated to do a such simple thing: download some bytes from a website.

As a "old" .net programmer I come from a world where the main philosophy is that easy problems should have easy solutions.  I don't want to multi-thread anything, I just want to call a method and wait until it has done its job. Since I use try...catch if the method fails I can get some error code and debug it. Right?

 

Looking forward to any reply, and thanks in advance for spending your time to read my post.

 

 

 

Carpe diem, always.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Sample HTTP Connection code and BIS-B Access

Sorry I have been trying to find time for a detailed reply since I think this question deserves it.  Failed so far, but here is a quick one. 

 

As the first post notes:

The code addresses the most common issues I have with samples that people provide

a) They don't get off the Event Thread

b) They don't log the actions that occur

 

It unfortunately also has to cope with the myriad of potential ways that the BlackBerry can obtain data, see this:

http://supportforums.blackberry.com/t5/Java-Development/Networking-Transports-II/ta-p/446742

 

You seem to have done (a), not so sure about (b) and you definitely don't cope with the myriad of potential connections.  So I suspect that explains why my code is more complicated than yours. 

 

Is it more complicated than it needs to be?  Definitely.  This is a long time complaint and why RIM introduced ConnectionFactory in OS 5.0 and made further networking improvements in OS 6.0.  So if you are targeting those releases, then you can have much simpler code.

 

"Have I been so lucky that I just made it work from the very first time?!?!  "

It is not just luck that has got you so far, you obviously understand networking better than a lot of people when they start their BB coding.  But I do think you have been lucky in that you have not had to cope with the variety of possible connection parameters and you haven't hit any of the common snags like character conversion.  And you are not trying to do too much like some people will try to parse the input from the request directly. 

 

So not just luck - skill!