Listening for events

Top  Previous  Next

For an object to listen for events published by the Http class the following steps are required:

 

1. Set object to implement HttpListener or extend HttpAdapter class.

2. Overload event handling methods.

3. Subscribe object to receive events published by Http instance.

 

The HttpListener class is a pure interface and can be used in cases where the class you defined to listen for events already extends another class type. The HttpAdapter class implements the HttpListener interface and can be used in cases where there is no need for class inheritance.

 

Note

 

Unless your class requires inheritance it is generally easier to use the HttpAdapter class as it provides default implementations for all the event handler methods defined in the HttpListener interface. This allows you to overload only the event handler methods that you are interested in.

 

Example

 

The example below demonstrates using the HttpListener class.

 

import com.jscape.inet.http.*;

 

public class MyHttpListener implements HttpListener {

 

 public void connected(HttpConnectedEvent event) {

         System.out.println("Connected to URL: " + event.getURL());

 

 }

 

 public void disconnected(HttpDisconnectedEvent event) {

         System.out.println("Disconnected from URL: " + event.getURL());

 

 }

 

 public void progress(HttpProgressEvent event) {

         // process event

 }

 

 public static void main(String[] args) {

         try {

                 // create new Http instance

                 Http http = new Http();

                 

                 // subscribe to events

                 http.addHttpListener(new MyHttpListener());

                 

                 // create request and get response

                 HttpRequest request = new HttpRequest("http://www.sun.com");

                 HttpResponse response = http.getResponse(request);

                 System.out.println(response.getBody());

                 

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

 

}

 

 

Example

 

The example below demonstrates using the HttpAdapter class.

 

import com.jscape.inet.http.*;

 

public class MyHttpAdapter extends HttpAdapter {

 

 public void connected(HttpConnectedEvent event) {

         System.out.println("Connected to URL: " + event.getURL());

 

 }

 

 public void disconnected(HttpDisconnectedEvent event) {

         System.out.println("Disconnected from URL: " + event.getURL());

 

 }

 

 public static void main(String[] args) {

         try {

                 // create new Http instnace

                 Http http = new Http();

                 

                 // subscribe to events

                 http.addHttpListener(new MyHttpAdapter());

                 

                 // create request and get response

                 HttpRequest request = new HttpRequest("http://www.sun.com");

                 HttpResponse response = http.getResponse(request);

                 System.out.println(response.getBody());

                 

         } catch(Exception e) {

                 e.printStackTrace();

         }

 }

 

 

}

 

 





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC