2015-09-14 93 views
1

我正在使用一个简单的main方法java类来调用一个restful URL来使用该应用程序。我创建了一个客户端并试图调用该URL。我的问题是:如何添加json数据作为查询参数?以下是我的方法。如何将json数据添加到客户端url

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(); 

     } 

    } 
+0

你想查询参数添加到另一台服务器的呼叫? –

+0

不,在这里我得到编译时error.please在这里修改url – Sthogari

+0

啊对不起,没有看到其余的查询字符串 –

回答

1

您需要在字符串文本中间加上引号:例如,使用\"NostudentsOrganized\"而不是仅仅使用"NostudentsOrganized"

+0

感谢您的回复现在我没有收到编译时错误,但我得到运行时错误java.lang.IllegalArgumentException:索引142处的查询中存在非法字符 – Sthogari

0

试试这个:

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 
相关问题