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
Trusted Contributor
foufou
Posts: 109
Registered: ‎11-15-2011
My Device: blackberry 9800

TOO MANY THREADS ERROR EXCEPTION

I'm facing to this exception "too many thread errors when i try to pass many urls to browser fields

 public static void webpage(int index) {

            String url;
            try {
                int size = 30;

                System.out.println(index);
                if (index < size && index > -1) {

                        url = get_URL();
                     new BF2CacheScreen(url,index);


                }

            } catch (Throwable t) {
            }



///////////////////browser/////////////////////:
package testcache;

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

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

import org.w3c.dom.Document;

import mc.bluewave.fext.mobile.Data.CacheItem;
import mc.bluewave.fext.mobile.Data.Controller;
import mc.bluewave.fext.mobile.Utilities.NetWorkUtilities;
import mc.bluewave.fext.mobile.Utilities.Utilities;
import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldConfig;
import net.rim.device.api.browser.field2.BrowserFieldController;
import net.rim.device.api.browser.field2.BrowserFieldListener;
import net.rim.device.api.browser.field2.BrowserFieldRequest;
import net.rim.device.api.browser.field2.ProtocolController;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.io.http.HttpHeaders;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

/**
 * BF2CacheSampleApp - A sample BrowserField2 application that implements a short-term Web cache
 * 
 * - Available at the BlackBerry Support Forum (http://supportforums.blackberry.com)
 *   as an attachment of the article entitled "How to Implement a Web Cache for Your 
 *   BrowserField2 Application".
 * 
 * Class BF2CacheSampleApp
 *  -This class extends the UiApplication class, providing a graphical user interface.
 */

/**
 * Class BF2CacheSampleScreen - A class extending the MainScreen class, which
 * provides default standard behavior for BlackBerry GUI applications.
 */
public final class BF2CacheScreen extends MainScreen {
    private BrowserField browserField;
    private TextField urlField;
    private String HTML = "";
    private String html_data = "";
    private byte[] data = null;
    private String content_type = "";
    private HttpConnection httpConn = null;
    private InputStream input = null;
    private Controller protocolController;
    int resCode;
    int index;
    private boolean clearURL = true;

    /**
     * Creates a new BF2CacheSampleScreen object
     */
    public BF2CacheScreen(final String url,final int index) {
        // Set the displayed title of the screen
        setTitle("BrowserField2 Cache Sample App");
        this.index=index;

        VerticalFieldManager manager = new VerticalFieldManager(
                Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT);
        try {
            httpConn = NetWorkUtilities.getHttpConnection(url);
            httpConn.setRequestProperty("Cookie",
                    NetWorkUtilities.Session_Cookies);

            httpConn.setRequestMethod(HttpConnection.GET);

            // Set Cookie
            NetWorkUtilities.setcookies(httpConn);

            input = httpConn.openInputStream();
            resCode = httpConn.getResponseCode();

            content_type = httpConn.getType();
            data = IOUtilities.streamToBytes(input);
            html_data = new String(data, "UTF-8");
            System.out.println(html_data + "********");
        } catch (Exception e) {
            // Hide Loading Screen

        } finally {
            // Close Connections
            try {
                if (input != null) {
                    input.close();
                    input = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (httpConn != null) {
                    httpConn.close();
                    httpConn = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        BrowserFieldConfig config = new BrowserFieldConfig();

        config.setProperty(BrowserFieldConfig.ALLOW_CS_XHR, Boolean.TRUE);
        config.setProperty(BrowserFieldConfig.ENABLE_COOKIES, Boolean.TRUE);
        config.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE);
        config.setProperty(BrowserFieldConfig.VIEWPORT_WIDTH, new Integer(
                Display.getWidth()));
        config.setProperty(BrowserFieldConfig.INITIAL_SCALE, new Float(1));

        config.setProperty(BrowserFieldConfig.USER_SCALABLE, Boolean.TRUE);

        HttpHeaders browserHttpHeaders = new HttpHeaders();

        browserHttpHeaders.addProperty(HttpHeaders.HEADER_ACCEPT_CHARSET,
                "UTF-8");


        browserHttpHeaders.addProperty(HttpHeaders.HEADER_COOKIE,
                NetWorkUtilities.Session_Cookies);

        config.setProperty(BrowserFieldConfig.HTTP_HEADERS, browserHttpHeaders);

        browserField = new BrowserField(config);
        browserField
                .displayContent(data, content_type + ";charset=UTF-8", HTML);

        config.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);

        BrowserFieldListener listener = new BrowserFieldListener() {

            public void documentLoaded(BrowserField browserField,
                    Document document) throws Exception {

                Utilities.webpage(index+1);
            }
        };
        browserField.addListener(listener);

    }
}

 

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

Re: TOO MANY THREADS ERROR EXCEPTION

Assuming that the bits of code we can't see (for example NetworkUtilities) don't use any Threads, then it seems the problem is simply that you are starting a large number of screens each with their own BrowserField.  I presume the BrowserField will maintain a set of Threads to download things like images and links, so the short answer would appear to be, don't start so many BrowserFields. 

 

What are you trying to achieve?  Are you just trying to preload screens for performance?

Trusted Contributor
foufou
Posts: 109
Registered: ‎11-15-2011
My Device: blackberry 9800

Re: TOO MANY THREADS ERROR EXCEPTION

I try to download ressources of web page of many urls. I don't witch thread it means the many threads. How can I go out to my problem?

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

Re: TOO MANY THREADS ERROR EXCEPTION

We can't  really answer that question without understanding why you are doing this.  We may give you some ideas, but then we don't know if they will work in your situation because, as noted, we don't know why you are doing this.

 

However what you seem to be doing is something I have never seen before and I can't think of a situation in which I would design an application that needed 30 or so BrowserFields. 

 

To give you a real world example, this is like asking someone to move1 ton of sugar.  You wouldn't ask someone to do this all at once.  You would ask them to carry it bit at a time. 

 

You are asking the BlackBerry to load up 30 BrowserFields, a more reasonable request would seem to be to load up 1 BrowserField 30 times.   

 

But I don't know if that will match what you need to do.  So let us we start by finding out what you are really trying to do.  Why do you need these BrowserFields all loaded?  How does that help you?  Please explain. 

Trusted Contributor
foufou
Posts: 109
Registered: ‎11-15-2011
My Device: blackberry 9800

Re: TOO MANY THREADS ERROR EXCEPTION

I told you that I have 

vector contain{url1, url2, url3..........}

for every url in this vector I want to have the ressources of web page

(css1,css11,css12html1,jpeg1......)for url1

(css2,css21,html2,jpeg2......)for url2

.........................

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

Re: TOO MANY THREADS ERROR EXCEPTION

No I'm confused.  If you want to just get at the resources, why are you using a BrowserField?

 

I still don't understand why you are doing this.  How is this going to help a user?

Trusted Contributor
foufou
Posts: 109
Registered: ‎11-15-2011
My Device: blackberry 9800

Re: TOO MANY THREADS ERROR EXCEPTION

Thanks for your reply.

How can I get the resources of web page to use this method "handleResourceRequest" without using browserField??

If there are another method please tell me. The use want to open web pages on offline that's why I want to save all resources.

I wait your answer thanks

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

Re: TOO MANY THREADS ERROR EXCEPTION

[ Edited ]

Ah OK.  So you are trying to provide the user with the ability to specify a web page, and have the BlackBerry save the web page and all the downloaded data yourself.  Then later they can display the same web page again without needing to be connected. 

 

I think you should initially write this to handle one single page.  The first question then is "how are you going to figure out what you need to download for this page?". 

 

I have never done this, but there are plenty of PC based tools that do this and I suspect if you search the internet you will find some helpful articles.

 

Without researching this much myself, I would suspect there were two options that you could use.

 

One option is to write your own light weight browser that parses the page and then extracts all the things that need to be downloaded.  There are some open source java parsers out there that might help with this, such as you will find here:

http://java-source.net/open-source/html-parsers

 

Another option is the one that you are attempting.  You want to open a page in a BrowserField, then download all the data for references that it makes and then save it.  This is pretty much exactly what the KB article:

http://supportforums.blackberry.com/t5/Java-Development/How-to-Implement-a-Web-Cache-for-Your-Browse...

does.  You seem to have used it as the basis for your application.  So I don't understand why you are not just saving the data that you download using super.handleResourceRequest in the handleResourceRequest() method of your ProtocolController.  You get all the data there, so that would seem a good place to do it.

 

So the approach you are taking is correct.  But you need one BrowserField and use your own ProtocolController to get the resources and save them.

 

I suggest you review the referenced KB article again with this information and see if you can figure out how it works now.

 

This method works for a single web page.  If you want to download pages that this links too, there is more involved. 

 

Edit: tidied up some of the English to make this clearer.