我有一个我建立的Web服务...我现在要做的是发送一个简单的请求,其中包含一个从Tapestry Web应用程序到该Web服务的json查询字符串。我四处搜索,大多数人都说使用Apache HttpClient来实现这一点。与HttpClient一起,我正在使用URIBuilder。
我试图发送的Json对象看起来像这样
{"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203- 555-5555"},"password":"dead"}
*我意识到用纯文本等发送密码的问题......
可以使用的网址(通过手动输入到Web浏览器进行测试,并且此Web服务已经为Android客户端和iOS客户端提供服务)看起来像这样
http:// ##.##.###.##/createuser?json={"user":{"userEmail":"jdoe@gmail.com","firstName":"John","lastName":"Doe","phone":"203-555-5555"},"password":"dead"}
这里是我在谷歌周围试图弄清楚为什么这不会工作的HttpClient代码。基本上我想要做的是用URIBuilder创建一个URI,然后用新建的URI构造一个HttpPost或HttpGet对象。但是URIBuilding过程中出现了问题。当我调试时,当我尝试设置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;当我跨过我在代码中评论的行时出现的错误是
[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;
我已经尝试了许多方法和对象的其他组合,以便将此请求正确发送到服务器,而且似乎没有任何工作。希望我能看到一些相对简单的东西。
预先感谢您提供的任何指导。