1

I want to use RequestBuilder to make HTTP requests in my PlayN project as described here: http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html#DevGuideHttpRequests

I added the tag in my module xml file:

but I still have the following compilation error:

The import com.google cannot be resolved

Is there something else I should do to make my project compile?

Here is the code:

import com.google.gwt.http.client.*;
...

String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

try {
    Request request = builder.sendRequest(null, new RequestCallback() {
        public void onError(Request request, Throwable exception) {
       // Couldn't connect to server (could be timeout, SOP violation, etc.)
}

public void onResponseReceived(Request request, Response response) {
  if (200 == response.getStatusCode()) {
      // Process the response in response.getText()
  } else {
    // Handle the error.  Can get the status text from response.getStatusText()
      }
    }
  });
} catch (RequestException e) {
  // Couldn't connect to server
}

1 Answers1

0

If you're using Maven for your build (which I suspect you may be), make absolutely sure that the following dependency is in your html/pom.xml

<dependency>
  <groupId>com.google.gwt</groupId>
  <artifactId>gwt-user</artifactId>
  <version>2.4.0</version>
  <scope>provided</scope>
</dependency>

You might need to change the version if you're using a version of GWT other than 2.4.0

Edit: Now that I know you are running a Java application (based on the comments below) and not a GWT application, you're likely going to need to make HTTP requests with something other than GWT's HTTP client. You'll want to remove the aforementioned dependency and take a look at the answers to this question for some insight into how to do that...

If you're needing to make HTTP requests in both the GWT and Java PlayN targets, you're likely going to need to abstract the HTTP client interface needed in the core module and provide the appropriate concrete implementations in the java and GWT modules. I describe using Gin and Guice to inject java and GWT specific instances of AsyncService<> objects in this answer here, and a similar approach can be used in injecting the appropriate HTTP client instance required on a per platform basis if necessary...

Community
  • 1
  • 1
hatboyzero
  • 1,842
  • 1
  • 21
  • 43
  • Now it compiles but I get a runtime exception: Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gwt/http/client/RequestException – Simon Dansereau Feb 08 '12 at 21:51
  • Sorry -- remove the `provided` line or change it to `runtime` and see if it works... – hatboyzero Feb 08 '12 at 21:58
  • Now I get: Exception in thread "main" java.lang.UnsatisfiedLinkError: com.google.gwt.xhr.client.XMLHttpRequest.create()Lcom/google/gwt/xhr/client/XMLHttpRequest; – Simon Dansereau Feb 08 '12 at 22:43
  • By the way, I am trying to run the project as a Java application. Maybe the mix between playN and GWT is unsupported here? – Simon Dansereau Feb 08 '12 at 22:44
  • heh, yeah -- you need to be making HTTP calls with something else. I'm betting that XMLHttpRequest.create() is making a JSNI call, which isn't going to work in Java. You might want to look at http://stackoverflow.com/a/1359700/302804 for some insight into making calls in a Java application... – hatboyzero Feb 08 '12 at 22:53