Java 如何将json数据添加到客户端url
我使用一个简单的主方法java类来调用restful URL来使用应用程序。我创建了一个客户端并尝试调用URL。我的问题是:如何添加json数据作为查询参数?下面是我的方法Java 如何将json数据添加到客户端url,java,json,rest,Java,Json,Rest,我使用一个简单的主方法java类来调用restful URL来使用应用程序。我创建了一个客户端并尝试调用URL。我的问题是:如何添加json数据作为查询参数?下面是我的方法 public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client.resource("http:
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client.resource("http://10.123.85.120:8080/myWebService/updateModel.do?abc=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0&jsonData={"NostudentsOrganized": 1,"Noofcourses": 20,"Noofattendedstudents": 5}");
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
你需要跳过字符串文字中间的引号:例如使用<代码> \“没有学生组织\”/“代码>而不是只是<代码>”,没有学生组织“> P>试试这个:
String jsonData = "{'NostudentsOrganized': 1,'Noofcourses': 20,'Noofattendedstudents': 5}";
String uri = "http://10.123.85.120:8080/myWebService/updateModel.do?abc=33589&applicationId=8&uuid=9a26038f-6dd1-40b6-b847-f2fd16366fc0&jsonData" + URLEncoder.encode(jsonData, "UTF-8");
WebResource webResource = client.resource(uri);
//rest of the code
是否要将查询参数添加到另一个服务器调用中?不,我在这里遇到编译时错误。请在此修改url啊,很抱歉,没有看到查询字符串的其余部分。您是否特别希望将其作为查询参数或是否可以发帖子?您应该作为帖子参数发送,因为GET的url长度有限。谢谢您的回复,我现在不需要了获取编译时错误,但获取运行时错误java.lang.IllegalArgumentException:索引142处的查询中的非法字符