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
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

HTTPFilter

Hello,

 

I need help implementing  an HTTPFilter.

I am just tryint to run an app similar to HttpFilterDemo and it does not work, when I try to browse to filtered

web address simulator open a dialog that says "invalid address http://...." what i want is to bring to foreground

the application with a specific screen.

 

This is my code:

Application

 

ackage mypackage;

import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;
import net.rim.device.api.io.http.HttpFilterRegistry;
import net.rim.device.api.ui.UiApplication;

/**
 * This class extends the UiApplication class, providing a
 * graphical user interface.
 */
public class MyApp extends UiApplication
{
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args)
    {
    	ApplicationPermissionsManager appPermMan = ApplicationPermissionsManager
				.getInstance();
		ApplicationPermissions permissions = appPermMan
				.getApplicationPermissions();
		permissions
				.addPermission(ApplicationPermissions.PERMISSION_BROWSER_FILTER);
		boolean b = appPermMan.invokePermissionsRequest(permissions);
    	HttpFilterRegistry.registerFilter("www.noticias24.com", "mypackage.");
        MyApp theApp = new MyApp();       
        theApp.enterEventDispatcher();
    }
    

    /**
     * Creates a new MyApp object
     */
    public MyApp()
    {        
        // Push a screen onto the UI stack for rendering.
        pushScreen(new MyScreen());
    }    
}

 

Screen

 

package mypackage;

import net.rim.blackberry.api.browser.Browser;
import net.rim.blackberry.api.browser.BrowserSession;
import net.rim.device.api.browser.field.RenderingOptions;
import net.rim.device.api.browser.field.RenderingSession;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen implements FieldChangeListener
{
	ButtonField boton;
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("MyTitle");
        add(new LabelField("aAAA"));
        boton = new ButtonField("Botón");
        boton.setChangeListener(this);
        add(boton);
		
    }

	public void fieldChanged(Field field, int context) {
		
		if(field == boton){
			BrowserSession myBrowser = Browser.getDefaultSession();
			myBrowser.displayPage("http://www.noticias24.com");
		}
		
	}
	
	public boolean onClose(){
		setDirty(false);
		return true;
	}
}

 Protocol

 

package mypackage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connection;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.io.FilterBaseInterface;
import net.rim.device.api.ui.component.Status;

public class Protocol implements FilterBaseInterface, HttpConnection{

    private HttpConnection _subConnection;

	public Connection openFilter(String name, int mode, boolean timeouts)
			throws IOException {
		
		return this;
	}
	
	 /**
     * @see javax.microedition.io.HttpConnection#getURL()
     */
    public String getURL()
    {
        return _subConnection.getURL();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getProtocol()
     */
    public String getProtocol()
    {
        return _subConnection.getProtocol();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getHost()
     */
    public String getHost()
    {
        return _subConnection.getHost();
    }


    /**
     * @see javax.microedition.io.HttpConnection#getFile()
     */
    public String getFile()
    {
        return _subConnection.getFile();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getRef()
     */
    public String getRef()
    {
        return _subConnection.getRef();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getQuery()
     */
    public String getQuery()
    {
        return _subConnection.getQuery();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getPort()
     */
    public int getPort()
    {
        return _subConnection.getPort();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getRequestMethod()
     */
    public String getRequestMethod()
    {
        return _subConnection.getRequestMethod();
    }

    /**
     * @see javax.microedition.io.HttpConnection#setRequestMethod(String)
     */
    public void setRequestMethod(String method) throws IOException
    {
        _subConnection.setRequestMethod(method);
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getRequestProperty(String)
     */
    public String getRequestProperty(String key)
    {
        return _subConnection.getRequestProperty(key);
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#setRequestProperty(String, String)
     */
    public void setRequestProperty(String key, String value) throws IOException
    {
        System.out.println("Request property <key, value>: " + key + ", " + value );
        _subConnection.setRequestProperty(key, value);
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getResponseCode()
     */
    public int getResponseCode() throws IOException
    {
        return _subConnection.getResponseCode();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getResponseMessage()
     */
    public String getResponseMessage() throws IOException
    {
        return _subConnection.getResponseMessage();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getExpiration()
     */
    public long getExpiration() throws IOException
    {
        return _subConnection.getExpiration();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getDate()
     */
    public long getDate() throws IOException
    {
        return _subConnection.getDate();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getLastModified()
     */
    public long getLastModified() throws IOException
    {
        return _subConnection.getLastModified();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getHeaderField(String)
     */
    public String getHeaderField(String name) throws IOException
    {
        String value = _subConnection.getHeaderField(name);
        System.out.println("Response property <key, value>: " + name + ", " + value );
        return value;
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getHeaderFieldInt(String, int)
     */
    public int getHeaderFieldInt(String name, int def) throws IOException
    {
        return _subConnection.getHeaderFieldInt(name, def);
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getHeaderFieldDate(String, long)
     */
    public long getHeaderFieldDate(String name, long def) throws IOException
    {
        return _subConnection.getHeaderFieldDate(name, def);
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getHeaderField(int)
     */
    public String getHeaderField(int n) throws IOException
    {
        return _subConnection.getHeaderField(n);
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getHeaderFieldKey(int)
     */
    public String getHeaderFieldKey(int n) throws IOException
    {
        return _subConnection.getHeaderFieldKey(n);
    }
    
 
    /**
     * @see javax.microedition.io.HttpConnection#getType()
     */
    public String getType()
    {
        return _subConnection.getType();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getEncoding()
     */
    public String getEncoding()
    {
        return _subConnection.getEncoding();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#getLength()
     */
    public long getLength()
    {
        return _subConnection.getLength();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#openInputStream()
     */
    public InputStream openInputStream() throws IOException
    {
        return _subConnection.openInputStream();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#openDataInputStream()
     */
    public DataInputStream openDataInputStream() throws IOException
    {
        return _subConnection.openDataInputStream();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#openOutputStream()
     */
    public OutputStream openOutputStream() throws IOException
    {
        return _subConnection.openOutputStream();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#openDataOutputStream()
     */
    public DataOutputStream openDataOutputStream() throws IOException
    {
        return _subConnection.openDataOutputStream();
    }
    

    /**
     * @see javax.microedition.io.HttpConnection#close()
     */
    public void close() throws IOException
    {
        _subConnection.close();
    }    


}

 

BlackBerry Development Advisor
Posts: 15,264
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: HTTPFilter

You'll need to call Application.requestForeground() from within your application to bring it into view.

 

This won't prevent the HTTP error from being shown in the browser.  You'll need to implement that in the Protocol class (solution will vary depending on what you want to occur).

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Re: HTTPFilter

Thanks for your answer,

 

If I put UiApplication.getUiApplication().requestForeground(); inside Protocol class

public class Protocol implements FilterBaseInterface, HttpConnection{

    private HttpConnection _subConnection;

	public Connection openFilter(String name, int mode, boolean timeouts)
			throws IOException {
		Application.getApplication().requestForeground();
		return this;
         }
}

 I got Uncaught exception: no application instance when launching app first time.

What I need is to close Browser a bring to foreground the aplication.

Developer
Posts: 246
Registered: ‎12-13-2010
My Device: Not Specified

Re: HTTPFilter

 

Move this into "public MyApp()":

HttpFilterRegistry.registerFilter("www.noticias24.com", "mypackage.");
BlackBerry Development Advisor
Posts: 15,264
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: HTTPFilter

UiApplication.getUiApplication() attempts to retrieve the application in the foreground.  If your application is in the background when you call this, you won't be getting an instance of your application.  You'll need to store a copy of your application that you instantiate in main.  Have a look at this example for an idea on the design, which does something similar.

 

Allow a background listener to detect and update a GUI application

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 100
Registered: ‎01-13-2011
My Device: Not Specified

Re: HTTPFilter

Thanks for your answer,

 

But I do not have Alternate CLDC Application Entry Point Application type option on my Application tab, only BlackBerry Application, MIDlet and Library

BlackBerry Development Advisor
Posts: 15,264
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: HTTPFilter

This page has the steps on how to create an alternate entry point in Eclipse.

 

Set up an alternate entry point for an application

 

I'll update that other article to point to this one.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker