Java listener

J

Jim Lee

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);
}
 
S

Stefan Ram

Jim Lee said:
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.
 
J

John B. Matthews

Jim Lee 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?

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>
 
M

markspace

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();
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top