1

I have a web service that I built... what I am trying to do now is send a simple request that contains a json query string from a Tapestry web app to that web service. I searched around and most people say to use Apache HttpClient to achieve this. Along with HttpClient I am using URIBuilder.

The Json object that I am trying to send looks like this

{"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}

*I realize the issues with the password being sent in plain text etc...

The url that works (tested by manually entering in a web browser and this web service already services an Android client and an iOS client) looks like this

http:// ##.##.###.##/createuser?json={"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203-555-5555"},"password":"dead"}

Here is the HttpClient code that I have mashed together from google'ing around trying to figure out why this wont work. Essentially what I am trying to do is create a URI with URIBuilder and then construct an HttpPost or HttpGet object with the newly built URI. But something is going wrong in the URIBuilding process. When I debug, an exception gets thrown when I try to set all the aspects of the URI.

Object onSuccess() throws ClientProtocolException, IOException, URISyntaxException{
    // json = {"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
    String json = user.toJson();
    URIBuilder builder = new URIBuilder();
    // Error gets thrown when I step over the next line
    builder.setScheme("http").setHost("##.###.##.###").setPort(8080).setPath("createuser").setQuery("json=" +json); 
    URI uri = builder.build();

    HttpPost request = new HttpPost(uri);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    String tmp = request.getURI().toString();

    HttpResponse response = httpClient.execute(request); 
    index.setResponse(EntityUtils.toString(response.getEntity()));
    return index;

The error that comes back when I step over the line that I commented in the code is

[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List; java.lang.NoSuchMethodError:org.apache.http.client.utils.URLEncodedUtils.parse(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/util/List;

I have tried a lot of other combinations of methods and objects to get this request to send off to the server correctly and nothing seems to work. Hopefully I am overlooking something relatively simple.

Thanks in advance for any guidance you can provide.

mfunaro
  • 574
  • 6
  • 23
  • That error means that you are using the wrong version of the HttpClient library. – Luciano Aug 10 '12 at 02:57
  • What version should I be using? I downloaded the zip file containing all the jars from here http://hc.apache.org/downloads.cgi – mfunaro Aug 10 '12 at 03:07
  • That would depend on the library that's calling the method. Check the documentation of that library to see if it lists its dependencies. – Luciano Aug 10 '12 at 03:13
  • The HttpClient does list its dependencies and those jars it is dependent upon are in the project as well. – mfunaro Aug 10 '12 at 03:17

1 Answers1

1

You most likely have the wrong version or two versions of the apache httpcomponents on your classpath. If you are running Tapestry it will print out all packages on the classpath on the error page. Investigate there, find which httpcomponents is loaded, figure out where it comes from and fix it.

If this does not work, you should share some of your runtime environment with us. Which servlet engine, running from which IDE or are you running from the command line. Are you using Maven? If so share your pom. Etc.

joostschouten
  • 3,863
  • 1
  • 18
  • 31
  • Is there anything fundamentally wrong with the code? I did notice that Tapestry seems to include HttpClient and HttpCore with their default project setup. So I have gotten past the original issue but I still cant seem to get a proper request out. – mfunaro Aug 13 '12 at 22:29
  • If you managed to solve the initial issue it sound to me your problem is solved and you have a new one which requires a bit more than "...still cant seem to get a proper request out". Please close this issue and start a new one with the changed code and exact problem/exception. Leave a comment here and I'll have a look. Good luck! – joostschouten Aug 14 '12 at 06:25