java WebService接口调用,传JSON参数

  1. 转自:http://zheyiw.iteye.com/blog/1571222



  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.OutputStreamWriter;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7.   
  8. public class Copy_2_of_PostDemo {  
  9.   
  10.     final static String url = "";  
  11.     final static String params = "{\"id\":\"12345\"}";  
  12.   
  13.     /** 
  14.      * 发送HttpPost请求 
  15.      *  
  16.      * @param strURL 
  17.      *            服务地址 
  18.      * @param params 
  19.      *            json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/> 
  20.      * @return 成功:返回json字符串<br/> 
  21.      */  
  22.     public static String post(String strURL, String params) {  
  23.         System.out.println(strURL);  
  24.         System.out.println(params);  
  25.         try {  
  26.             URL url = new URL(strURL);// 创建连接  
  27.             HttpURLConnection connection = (HttpURLConnection) url  
  28.                     .openConnection();  
  29.             connection.setDoOutput(true);  
  30.             connection.setDoInput(true);  
  31.             connection.setUseCaches(false);  
  32.             connection.setInstanceFollowRedirects(true);  
  33.             connection.setRequestMethod("POST"); // 设置请求方式  
  34.             connection.setRequestProperty("Accept""application/json"); // 设置接收数据的格式  
  35.             connection.setRequestProperty("Content-Type""application/json"); // 设置发送数据的格式  
  36.             connection.connect();  
  37.             OutputStreamWriter out = new OutputStreamWriter(  
  38.                     connection.getOutputStream(), "UTF-8"); // utf-8编码  
  39.             out.append(params);  
  40.             out.flush();  
  41.             out.close();  
  42.             // 读取响应  
  43.             int length = (int) connection.getContentLength();// 获取长度  
  44.             InputStream is = connection.getInputStream();  
  45.             if (length != -1) {  
  46.                 byte[] data = new byte[length];  
  47.                 byte[] temp = new byte[512];  
  48.                 int readLen = 0;  
  49.                 int destPos = 0;  
  50.                 while ((readLen = is.read(temp)) > 0) {  
  51.                     System.arraycopy(temp, 0, data, destPos, readLen);  
  52.                     destPos += readLen;  
  53.                 }  
  54.                 String result = new String(data, "UTF-8"); // utf-8编码  
  55.                 System.out.println(result);  
  56.                 return result;  
  57.             }  
  58.         } catch (IOException e) {  
  59.             // TODO Auto-generated catch block  
  60.             e.printStackTrace();  
  61.         }  
  62.         return "error"// 自定义错误信息  
  63.     }  
  64.   
  65.     public static void main(String[] args) {  
  66.         post(url, params);  
  67.     }  
  68.   
  69. }  
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java调用Web服务接口JSON参数,可以使用Java标准库提供的类库来实现。 首先,你需要使用Java的HTTP客户端来发送HTTP请求。推荐使用Apache HttpClient库,它提供了丰富的API和功能。 以下是一些发送HTTP请求的基本步骤: 1. 创建一个HttpClient实例,可以使用 HttpClientBuilder.create().build() 来创建: HttpClient client = HttpClientBuilder.create().build(); 2. 创建一个HttpPost对象,并设置请求的URL: HttpPost post = new HttpPost("http://example.com/api"); 3. 设置请求头,指定请求的内容类型为application/json: post.setHeader("Content-Type", "application/json"); 4. 创建一个JSON对象,用于存储要递的参数JSONObject json = new JSONObject(); json.put("param1", "value1"); json.put("param2", "value2"); 5. 构建HttpEntity对象,将参数JSON字符串的形式放入请求的正文中: StringEntity entity = new StringEntity(json.toString()); post.setEntity(entity); 6. 发送请求并获取响应: HttpResponse response = client.execute(post); 7. 解析响应的内容: InputStream inputStream = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } String responseJson = stringBuilder.toString(); 注意,以上步骤只是实现了一个基本的HTTP请求,如果需要进行身份验证、添加请求头等更复杂的操作,你可以进一步研究HttpClient库的文档。 最后,解析响应的内容时需要根据接口的返回类型进行具体的处理。 希望以上解答对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值