Hi,

I am trying to consume a REStful web service from my web application
that is deployed under the App Engine.
I am using the Jersey framework in order to make the web service call
and I followed the steps outlined in this tutorial:
http://blogs.sun.com/enterprisetechtips/entry/consuming_restful_web_services_with.
I need to call this web service periodically and gather data which is
then entered into the App Engine data store.

The following is what I do:

In have an instance of  javax.servlet.ServletContextListener which is
called upon the application start up. It spins up a background thread
by calling the start() method on an instance of a class that extends
java.lang.Thread. Then, inside the run() method of the class that
extends Thread, I have the code for calling the web service. Here's
that code. Below, the classes Client, ClientConfig,
DefaultClientConfig and WebResource are all from the Jersey framework.

                try
                {
                        ClientConfig config = new DefaultClientConfig ();
                        Client client = Client.create (config);
                        WebResource webResource = client.resource 
("http://www.iobridge.com/
interface/feed/FD_0BosKdymNbB3LyiFYUVI");
                        String response = webResource.accept 
("application/json").get
(String.class);
                }
                catch (Exception ex)
                {
                }


Here's my problem:

When I execute this code on a background thread, I get a
NullPointerException. However, if I execute it, say, in the
ServletContextListener's contextInitialized() method, then it works
fine. What is causing this to happen?

I repeated this experiment by trying to make an HTTP GET request using
plain old Java classes in the java.net package, that is, using the URL
and URLConnection classes.

URL url = new URL ("http://www.iobridge.com/interface/feed/
FD_0BosKdymNbB3LyiFYUVI");
URLConnection connection = url.openConnection ();
connection.setDoOutput (true);
connection.setRequestProperty ("Content-Type", "application/json");
BufferedReader in = new BufferedReader (new InputStreamReader
(connection.getInputStream ()));


Here too, the code does not work on a background thread but works OK
elsewhere.

Is the usage of background threads the primary cause here?

Thanks,
Viji




-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to