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
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar
Accepted Solution

PleaseWaitPopupScreen

Hello I am using the code publishing in http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/t...

I my case I have a service class that calls the connecction and process the info letting in a vector.

My code is:

 

 

FieldChangeListener listener = new FieldChangeListener() {
   public void fieldChanged(Field field, int context) {
	ServiceHelper serviceHelper = new ServiceHelper();
        Vector result = serviceHelper.getProductList();
	 Dialog.alert("Hello There");
	 if (result.isEmpty()) {
	    Dialog.alert("No data found");
	 } else {
	   String jsonString = (String) result.elementAt(0);
	    Dialog.alert(jsonString);
	 }
}				 

 In the ServiceHelper class I have:

 

public Vector getProductList() {
  ConnectionThread ct = new ConnectionThread(
	Constants.ACTION_GET_PRODUCT_LIST);
	
  PleaseWaitPopupScreen.showScreenAndWait(ct, "Getting product list...");
  Vector responseVector = new Vector();
  if (ct.getResponseCode() == HttpConnection.HTTP_OK) {
	responseVector = ct.getResponseVector();
	Dialog.alert((String) responseVector.elementAt(0));
  } 
  return responseVector;
}

But the PleaseWaitPopupScreen does not wait !!! It show inmediately the "No data found"message and over it the PleaseWaiitScreen...

 

Any Hint? 

 

 

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

Re: PleaseWaitPopupScreen

I think we need to see the code for ConnectionThread to comment.  Can we see that?

Developer
Posts: 148
Registered: ‎11-16-2009
My Device: Not Specified

Re: PleaseWaitPopupScreen

Hello

I think the problem is you don`t wait enough and you check the response code. I think the ConnectionThread is a thread as the name suggest it. So basically you start a thread, and it will download separate, but after that (mean 1-2 sec) you check the response code, so you don`t let enough time for downloading...

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

Re: PleaseWaitPopupScreen

[ Edited ]

Sannyo,

 

If you review the linked article, you will see that the

showScreenAndWait

method invokes the ct Thread as a Runnable, and so it is run as a separate Thread, even though it is not obvious from this code. 

 

Edit:  No I was wrong, which is very sad, since it was originally my code.  The old brain is going.  So I'm talking tosh.  Ignore this post. 

Developer
Posts: 148
Registered: ‎11-16-2009
My Device: Not Specified

Re: PleaseWaitPopupScreen

[ Edited ]

Maybe i`ve  expressed  myself very well.

So yes you are rigth it runs in another thread, but as i see ( i don`t compiled the code)  the problem the thread is started, the popupscreen is displayed the user can`t do anything, but the code will run to check the response code, however it takes around 1-2 second so response can`t be arrived that soon, that is the reason why he don`t get any data...

 

 

public Vector getProductList() {
//point 1
ConnectionThread ct = new ConnectionThread(
Constants.ACTION_GET_PRODUCT_LIST);

PleaseWaitPopupScreen.showScreenAndWait(ct, "Getting product list...");
Vector responseVector = new Vector();
//point 2
if (ct.getResponseCode() == HttpConnection.HTTP_OK) {
responseVector = ct.getResponseVector();
Dialog.alert((String) responseVector.elementAt(0));
}
return responseVector;
}

 

So between the 1st point and the 2nd point elapse only 1-2 seconds, so the download thread can`t finish the downloading. Maybe in this case would be good to use a command pattern to achieve what he want.

 

 

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

Re: PleaseWaitPopupScreen

[ Edited ]

Edit: As noted above, Sannyo is correct, I was talking twoddle.  Ignore this post

 

Between point 1 and point 2

 

PleaseWaitPopupScreen.showScreenAndWait(...)

 

is called. 

 

This will display a popup screen, and then stall, waiting for the Runnable to complete.  So code following this is not executed until the popupscreen has been dismissed.  I believe this statement is not correct:

"the code will run to check the response code"

as the code stops in showScreenAndWait(...) - hence the name.....

 

So the length of time that occurs between point 1 and point 2 is determined by the runnable, in this case ct.  If only 1-2 seconds have elapsed, the that is how long the runnable code in ct took to complete.  ct is a Thread, but is used as a Runnable in this case. 

 

Please review the information in the referenced article for more information.

 

The issue for me is that ct Thread is not processing correctly, which is why I asked to see that code. 

Developer
Posts: 148
Registered: ‎11-16-2009
My Device: Not Specified

Re: PleaseWaitPopupScreen

OK, i just looked into the code.

I found in the TestScreen.java src:

"

PleaseWaitPopupScreen.showScreenAndWait(getRequest, "Getting URL " + requestedURL);
            // This will not wait here while the screen is displayed
            // But the user will be blocked until the popup screen is dismissed.

"

Anyway i don`t argue with you, and be honest i don`t have much time to review/try your code.

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

Re: PleaseWaitPopupScreen

[ Edited ]

Profuse apologies sannyo, you are correct..

 

To the original poster, what sannyo said is correct, to use this screen you need to restructure how it is invoked.  Can we see your ConnectionThread class and I will suggest how this code can be changed to use the please wait screen correctly. 

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: PleaseWaitPopupScreen

First of all, thanks guys...

Peter, here is the run method of the ConnectionThread:

 

 

public class ConnectionThread extends Thread {
   .... 
  public void run() {
     HttpConnection connection = null;
     try {
	_running = true;
	connection = (HttpConnection) _manager.getConnection(_url,
					Connector.READ_WRITE, true);
	connection.setRequestProperty("User-Agent",
			"Profile/MIDP-2.0 Configuration/CLDC-1.0");
	connection.setRequestProperty("Accept_Language", "en-US");
	connection.setRequestMethod(HttpConnection.POST);
	connection.setRequestProperty("Content-type",
			"application/x-www-form-urlencoded");
	connection.setRequestProperty("Content-Length", Integer
				.toString(_postData.length));

	OutputStream out = connection.openOutputStream();
	out.write(_postData);
	out.close();

	_responseCode = connection.getResponseCode();
	if (_responseCode == HttpConnection.HTTP_OK) {
	_contentType = connection.getHeaderField("Content-type");
	_contentLength = connection.getLength();
	InputStream is = connection.openInputStream();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	byte[] buffer = new byte[BUFFER_SIZE];
	int bytesRead = is.read(buffer);
	while (bytesRead > 0) {
	  baos.write(buffer, 0, bytesRead);
	  bytesRead = is.read(buffer);
	}

	baos.flush();
	baos.close();

	decodeResponse(baos.toByteArray());
     }
 } catch (IOException e) {
   	_responseCode = HttpConnection.HTTP_INTERNAL_ERROR;
	_responseMessage = "IOException - URL :" + _url + "-"
				+ e.getMessage();
  } catch (Throwable t) {
	_responseCode = HttpConnection.HTTP_CLIENT_TIMEOUT;
	_responseMessage = "Throwable:" + t.getMessage();
  } finally {
	if (connection != null) {
    	  try {
		connection.close();
		connection = null;
	  } catch (IOException e) {}
  }
  _running = false;
 }
}

 

 

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

Re: PleaseWaitPopupScreen

I think in fact my article needs to be updated.  I think the way you have tried to use it, (i.e. invoking it as a blocking method), would in fact make it a great deal easier to use. .

 

Anyway, I'd like you to try something.  Currently the showScreenAndWait(..) method is the following:

 

    public static void showScreenAndWait(final Runnable runThis, String text) {
        final PleaseWaitPopupScreen thisScreen = new PleaseWaitPopupScreen(text);
        Thread threadToRun = new Thread() {
            public void run() {
                // First, display this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().pushScreen(thisScreen);
                    }
                });
                // Now run the code that must be executed in the Background
                try {
                    runThis.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                    throw new RuntimeException("Exception detected while waiting: " + t.toString());
                }
                // Now dismiss this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().popScreen(thisScreen);
                    }
                });
            }
        };
        threadToRun.start();
    }

 

 

Please change it to the following:

 

    public static void showScreenAndWait(final Runnable runThis, String text) {
        final PleaseWaitPopupScreen thisScreen = new PleaseWaitPopupScreen(text);
        /*
        Thread threadToRun = new Thread() {
            public void run() {
                // First, display this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().pushScreen(thisScreen);
                    }
                });
                */
        Thread threadToRun = new Thread() {
            public void run() {
                // Run the code that must be executed in the Background
                try {
                    runThis.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                    throw new RuntimeException("Exception detected while waiting: " + t.toString());
                }
                while ( !thisScreen.isDisplayed() ) {
                    try {
                       Thread.sleep(500);
                    } catch (Exception e) {
                    }
                }
                // Now dismiss this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().popScreen(thisScreen);
                    }
                });
            }
        };
        threadToRun.start();
        UiApplication.getUiApplication().pushModalScreen(thisScreen);
    }

 

 

This will mean that the code does, in fact wait in showScreenAndWait(..).  I didn't code it this way originally because it is slightly more complicated to explain. But I think it is 100% easier to use.  I think a little more explanation is a small price to pay for ease of use.

 

Anyway if it works for you, then I will update the articles.