0

My requirement is to parse two urls at application start point, these two urls have data that is required to be displayed in my application. I am doing this by keeping two urls in a array and running a for loop in the background thread and then insert the values into database in background thread, is it correct way of approaching the problem?

I have posted my code below, help of any kind is welcomed :)

   public StartConnecton(SplashScreen splashScreen)
{
    urls = new String[2];
    urls[0] = "http:xxxxxx.com";
    urls[1] = "http:yyy.com";
    _dbIRef = new ClassDatabase(1);
    _dbIRef.setSID(46);
    _splashScreen = (SplashScreen)splashScreen;
     _classDatabase = new ClassDatabase();

}

public void run()
{
    int size = urls.length;
    for(int i = 0; i < size;i++)
    {
        if(i==0)
        {
            _id= 1;
        }else if(i==1)
        {
            _id = 0;
        }

try{
    String conn = this.getConnectionString();
        con = (HttpConnection)Connector.open(urls[i]+getConnectionString());
        con.setRequestMethod(HttpConnection.GET);
        con.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-      1.0");
System.out.println("CONNECTION!!!!!!!!!!!"+con);
        code = con.getResponseCode();

System.out.println("CODE!!!!!!!!!!!"+code+"ID"+_id);         
if ( code == HttpConnection.HTTP_OK)
        {
             is = con.openInputStream();
             int length = (int) con.getLength();
             new Parser(is,_id);
             is.close();
             con.close();
        }   
    }catch(Exception e)
    {

System.out.println("EXCEPTION!!!!!!!!!!"+e);
    }
    }
    _classDatabase.delete("Delete from topnews where sid = 46");
    _classDatabase.insertTopNews();
     _classDatabase.insertTabBar();
     _classDatabase.insertGalleryInfo();

    _topNewsScreen = new TopNewsScreen("TopNews");
    _splashScreen.swapScreen(_topNewsScreen);



}

Help of any kind is welcomed

A Y

varunrao321
  • 925
  • 2
  • 10
  • 18
  • I think that when the `RequestMethod` is `GET` then the url should contain something like `www.site.com?variable=value` – pheromix Nov 17 '11 at 07:46
  • yes, I cannot expose the url so I kept something dummy, but my concern is if I am correct in what I am doing. is it correct methods to call multiple urls? – varunrao321 Nov 17 '11 at 07:47
  • If you know the Multi-threading concept then it is easy to do that. – alishaik786 Nov 17 '11 at 11:39
  • thanks for your reply alishaik, I would like to learn about maintaining threadpool..have you come across any example? – varunrao321 Nov 17 '11 at 12:17
  • You don't need to implement a Thread Pool in Blackberry, just spawning threads for making the connections. Also there's a limited number of threads you can spawn in BB, but to make only two connections you don't have to worry about that. A worker thread is enough. – Mister Smith Nov 17 '11 at 16:52

1 Answers1

2

The problems you have at the moment are:

1. The connections are instantiated sequentially. If the first one fails (server not there, BlackBerry MDS servers down, etc) then you'll have to wait around 30 seconds for the connection.open request to timeout before the second connection is tried.

2. The UI will freeze during connection attempts. I'm guessing you're doing this on the event thread as well, which means the app will freeze whilst the Connection.open is running because this method blocks.

The solution to both of the above problems is to wrap each connection attempt into a separate Thread. Here's a nice example: http://mnarinsky.blogspot.com/2011/03/blackberry-sending-http-request-in.html

3. Redundant code What is that if(i==0) block of code doing? If all you're trying to do is make _id = 1 when i == 0, then just do _id = (i==0) ? 1 : 0;. Alternatively reverse the order which you put the URLs into your array and just use i, and remove the _id variable entirely.

donturner
  • 17,867
  • 8
  • 59
  • 81
  • thanks for your reply donturner, I am not performing this on a event thread, I am doing this on a separate thread, I will refer that blog, thanks for the link, and I will change that code :) – varunrao321 Nov 18 '11 at 06:29