Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8575295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:39:40+00:00 2026-06-11T19:39:40+00:00

I am new to blackberry development and I am creating a native blackberry application.

  • 0

I am new to blackberry development and I am creating a native blackberry application. On every screen of my application, I need to send and receive data to the server on the same connection.

What I have done so far is I have made a ConnectToServer class which has a bunch of methods for sending and receiving. I instantiate it on the main screen and I pass it to each screen as a parameter.

That class in not a thread because I only read and write when the user types in information and presses a button. So basically I am using the inputStream and outputStream on the event thread which I hear is BAD. Then I ask ConnectToServer to get me what the server sent. For instance, I get a vector which I use to make a ListField.

How can I make these UI updates?

public class Screen3 extends MainScreen {

  ConnectToServer con;
  Vector v;

  public Screen3(String exerciseName, ConnectToServer connect)
  {
      con = connect;
      con.send(exerciseName);
      v = con.receiveVector();

      mylist = new listField();
      mylist.setSize(v.size());
      add(mylist);
   }

   public void drawListRow(...)
   {
      graphics.drawText((String) v.elementAt(index)
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:39:42+00:00Added an answer on June 11, 2026 at 7:39 pm

    So, there’s many ways to approach this. First of all, since it seems like you only want one instance of ConnectToServer, and you are currently having to pass that around, you might try making that class a Singleton object. This is not necessary, and does not have anything to do with your threading problem, but I only offer it as a solution, for situations where you want to enforce that there’s only one instance of something, and want to avoid having to pass it around everywhere. A simple Singleton implementation might be this:

    public class ConnectToServer {
    
        private static ConnectToServer _instance;
    
        /** use this static method to get the one and only instance */
        public static ConnectToServer getInstance() {
            if (_instance == null) {
                _instance = new ConnectToServer();
            }
            return _instance;
        }
    
        /** private to enforce Singleton pattern */
        private ConnectToServer() {
        }
    }
    

    And use it in your screens like this (no need to pass it into the constructor any more):

    ConnectoToServer connection = ConnectToServer.getInstance();
    connection.blahBlahBlah();
    

    Now, on to the threading problem. You’re right that you should not be performing network requests on the main (aka “UI”, aka “Event”) thread. If you have a nice separate ConnectToServer class, that makes it easier to encapsulate this behaviour. Instead of UI clients using a synchronous send() and receiveVector() method, make one method that just kicks off the request, and another callback method that the ConnectToServer class will call when the response comes back. The ConnectToServer class will use a Thread to perform this work, and thus avoid freezing the UI during the request.

    I’ll define an interface that the UI clients will implement:

    public interface RequestListener {
    
        /** listeners must implement this method to get data.  method will be called on the UI thread */
        void onDataReceived(Vector response);
    }
    

    And then the new (partial) ConnectToServer class:

    public class ConnectToServer {
        private Thread _worker;
        private RequestListener _listener;
    
        public void setRequestListener(RequestListener listener) {
            // note: this implementation only allows one listener at once.  
            //  make it a list if you need something more
            _listener = listener;
        }
    
        /** initiate a network request on a background thread */
        public void sendRequest(final String request) {
            _worker = new Thread(new Runnable() {
                public void run() {    // run on the background/worker thread
                    send(request);
    
                    final Vector response = receiveVector();
    
                    if (_listener != null) {
                        // this assumes all our listeners are UI objects, so we pass 
                        //   data back to them  on the UI thread:
                        UiApplication.getUiApplication().invokeLater(new Runnable() {
                            public void run() {    // run on UI thread                
                                _listener.onDataReceived(response);
                            }
                        });
                    }
                }
            });
    
            _worker.start();
        }
    }
    

    Note that you should also make your original send() and receiveVector() methods in this class private. They should only be called from inside the class now, not directly from UI clients.

    Then, you need to code your Screen classes like this:

    public class Screen3 extends MainScreen implements RequestListener {
    
        public Screen3(String exerciseName) {
            ConnectToServer connection = ConnectToServer.getInstance();
            connection.setRequestListener(this);
            // kick off the request (on a background thread)
            connection.sendRequest(exerciseName);
        }
    
        public void onDataReceived(Vector response) {
             if (mylist == null) {
                 // first time data has been received, so create and add the list field:
                 mylist = new listField();            
                 add(mylist);
             }
             mylist.setSize(response.size());
             // TODO: presumably, you would copy the contents of 'response' into 'mylist' here  
        }
    }
    

    Also, you might also want to code the server class to protect against multiple UI clients making concurrent requests, allow current requests to be cancelled, etc. But the above should get you started on a solution that provides a responsive app, without freezing your UI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Blackberry application development and I need a piece of advise. I
i am new in blackberry development. and i have task to develop the application
i am new to Blackberry application development. now i am trying to make new
I'm new to Blackberry development and need some direction on distributing an app to
I'm a BlackBerry native applications developer. I'm new in BB10 apps development. I'm supposed
I am new to Blackberry development.My application contain five list items having strings each
hi I m new in development blackberry application. I want post email and password
I am new to blackberry application development, i want to integrate Facebook into my
I'm new to Blackberry development and have come across something I don't understand. I
I'm new to blackberry development. There is this question I've come across several times,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.