Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10998 > unrolled thread

Java listener

Started byJim Lee <jimlee2907@yahoo.com>
First post2011-12-25 23:12 -0800
Last post2011-12-27 12:33 -0800
Articles 5 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Java listener Jim Lee <jimlee2907@yahoo.com> - 2011-12-25 23:12 -0800
    Re: Java listener "John B. Matthews" <nospam@nospam.invalid> - 2011-12-26 10:58 -0500
      Re: Java listener Arne Vajhøj <arne@vajhoej.dk> - 2011-12-27 17:59 -0500
    Re: Java listener markspace <-@.> - 2011-12-26 08:12 -0800
    Re: Java listener Roedy Green <see_website@mindprod.com.invalid> - 2011-12-27 12:33 -0800

#10998 — Java listener

FromJim Lee <jimlee2907@yahoo.com>
Date2011-12-25 23:12 -0800
SubjectJava listener
Message-ID<9a7gf750th7582hnjvfv9n0iko35omb57o@4ax.com>
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);
}

[toc] | [next] | [standalone]


#11000

From"John B. Matthews" <nospam@nospam.invalid>
Date2011-12-26 10:58 -0500
Message-ID<nospam-BE0DF7.10580026122011@news.aioe.org>
In reply to#10998
In article <9a7gf750th7582hnjvfv9n0iko35omb57o@4ax.com>,
 Jim Lee <jimlee2907@yahoo.com> 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>

[toc] | [prev] | [next] | [standalone]


#11012

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-12-27 17:59 -0500
Message-ID<4efa4de3$0$294$14726298@news.sunsite.dk>
In reply to#11000
On 12/26/2011 10:58 AM, John B. Matthews wrote:
> In article<9a7gf750th7582hnjvfv9n0iko35omb57o@4ax.com>,
>   Jim Lee<jimlee2907@yahoo.com>  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

[toc] | [prev] | [next] | [standalone]


#11001

Frommarkspace <-@.>
Date2011-12-26 08:12 -0800
Message-ID<jda6d8$ij0$1@dont-email.me>
In reply to#10998
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);
> }
>
> =====================================

[toc] | [prev] | [next] | [standalone]


#11011

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-12-27 12:33 -0800
Message-ID<rqakf7d8t6m2ofncucom4evrgcb3uug2ev@4ax.com>
In reply to#10998
On Sun, 25 Dec 2011 23:12:55 -0800, Jim Lee <jimlee2907@yahoo.com>
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.
 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web