Java listener

Discussion in 'Java' started by Jim Lee, Dec 26, 2011.

  1. Jim Lee

    Jim Lee Guest

    I have a java interface "ConnectionListener" ...

    And a method "httpPost" which pass a "ConnectionListener" as parameter

    How do I implement the ConnectionListener interface and how do I pass
    it into "httpPost" method to get invoked when "listener event" get
    response from the httpPost?

    =====================================

    public void httpPost(String url, List<Header> headers, HttpEntity
    body, ConnectionListener listener) {
    HttpPostTask task = new HttpPostTask(url, headers, body,
    listener);
    mExecutor.submit(task);
    }

    =====================================
    import java.io.InputStream;
    import org.apache.http.HttpMessage;

    public interface ConnectionListener {

    public void onConnection(int status, InputStream is,
    HttpMessage message);

    public void onConnectionException(Exception e);
    }
     
    Jim Lee, Dec 26, 2011
    #1
    1. Advertising

  2. Jim Lee

    Stefan Ram Guest

    Jim Lee <> writes:
    >And a method "httpPost" which pass a "ConnectionListener" as parameter


    »Passes« or »is passed«.

    Argument values are passed, not parameters.

    >How do I implement the ConnectionListener interface


    By declaring and implementing all its method signatures and
    its contract using public methods of a class C, which also
    declares that it implements this interface.

    >and how do I pass it into "httpPost" method


    One does not pass interfaces at all. One can pass objects,
    of a class that implements an interface. So here, one would
    pass a properly created instance of the class C.

    >to get invoked when "listener event" get response from the
    >httpPost?
     
    Stefan Ram, Dec 26, 2011
    #2
    1. Advertising

  3. In article <>,
    Jim Lee <> wrote:

    > I have a java interface "ConnectionListener" ...
    >
    > And a method "httpPost" which pass a "ConnectionListener" as
    > parameter
    >
    > How do I implement the ConnectionListener interface and how do I pass
    > it into "httpPost" method to get invoked when "listener event" get
    > response from the httpPost?


    The approach outlined in the EventListenerList [1] API may be suitable.
    It's used throughout Swing and related libraries such as JFreeChart.
    The scheme is a fairly general example of using a class literal as a
    run time type token [2].

    Also consider an existing Java HTTP connection library [3].

    [1] <http://docs.oracle.com/javase/7/docs/api/javax/swing/event/EventListenerList.html>
    [2] <http://docs.oracle.com/javase/tutorial/extra/generics/literals.html>
    [3] <http://www.google.com/search?q=java+http+connection+library>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Dec 26, 2011
    #3
  4. Jim Lee

    markspace Guest

    On 12/25/2011 11:12 PM, Jim Lee wrote:

    > How do I implement the ConnectionListener interface and how do I pass
    > it into "httpPost" method to get invoked when "listener event" get
    > response from the httpPost?



    1. Sub-class ConnectionListener to make a concrete class
    2. Create an object of that concrete class to pass to httpPost.

    As Stefan pointed out, interfaces are not passed as arguments, objects
    are, so you need to get an object to pass as a parameter.


    public class MyListener implements ConnectionListener {

    public void onConnection(int status, InputStream is,
    HttpMessage message) {
    System.out.println( "onConnection invoked" );
    }

    public void onConnectionException(Exception e){
    System.out.println( "onConnectionException invoked" ) {
    }
    }


    > =====================================
    >
    > public void httpPost(String url, List<Header> headers, HttpEntity
    > body, ConnectionListener listener) {


    MyListener listener = new MyListener();
    > HttpPostTask task = new HttpPostTask(url, headers, body,
    > listener);
    > mExecutor.submit(task);
    > }
    >
    > =====================================
     
    markspace, Dec 26, 2011
    #4
  5. Jim Lee

    Roedy Green Guest

    On Sun, 25 Dec 2011 23:12:55 -0800, Jim Lee <>
    wrote, quoted or indirectly quoted someone who said :

    >I have a java interface "ConnectionListener" ...
    >
    >And a method "httpPost" which pass a "ConnectionListener" as parameter
    >
    >How do I implement the ConnectionListener interface and how do I pass
    >it into "httpPost" method to get invoked when "listener event" get
    >response from the httpPost?
    >
    >=====================================
    >
    >public void httpPost(String url, List<Header> headers, HttpEntity
    >body, ConnectionListener listener) {
    > HttpPostTask task = new HttpPostTask(url, headers, body,
    >listener);
    > mExecutor.submit(task);
    >}
    >
    >=====================================
    >import java.io.InputStream;
    >import org.apache.http.HttpMessage;
    >
    >public interface ConnectionListener {
    >
    > public void onConnection(int status, InputStream is,
    >HttpMessage message);
    >
    > public void onConnectionException(Exception e);
    >}


    You might use somebody else's code, such as Apache HTTPClient or mine
    http://mindprod.com/products.html#HTTP
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    If you can't remember the name of some method,
    consider changing it to something you can remember.
     
    Roedy Green, Dec 27, 2011
    #5
  6. On 12/26/2011 10:58 AM, John B. Matthews wrote:
    > In article<>,
    > Jim Lee<> wrote:
    >> I have a java interface "ConnectionListener" ...
    >>
    >> And a method "httpPost" which pass a "ConnectionListener" as
    >> parameter


    > Also consider an existing Java HTTP connection library [3].


    > [3]<http://www.google.com/search?q=java+http+connection+library>


    He seems to already be using Apache HttpClient.

    Arne
     
    Arne Vajhøj, Dec 27, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danish Ahmed

    .net remoting and tcp listener socket

    Danish Ahmed, Apr 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,798
    Danish Ahmed
    Apr 21, 2005
  2. not entered

    Socket Listener not available

    not entered, Mar 31, 2005, in forum: Perl
    Replies:
    1
    Views:
    2,723
    Joe Smith
    Mar 31, 2005
  3. Mmm_moo_cows

    Java Event Listener integer problem.

    Mmm_moo_cows, Oct 24, 2004, in forum: Java
    Replies:
    4
    Views:
    825
    Mmm_moo_cows
    Oct 26, 2004
  4. Replies:
    4
    Views:
    601
  5. Thiago Pillar
    Replies:
    0
    Views:
    103
    Thiago Pillar
    Nov 16, 2008
Loading...

Share This Page