zoukankan      html  css  js  c++  java
  • java调用http接口并返回json对象

     1 import java.io.BufferedReader;
     2 import java.io.InputStreamReader;
     3 import java.io.OutputStreamWriter;
     4 import java.io.PrintWriter;
     5 import java.net.URL;
     6 import java.net.URLConnection;
     7 import java.net.URLEncoder;
     8 import java.util.HashMap;
     9 import java.util.Map;
    10 
    11 import javax.servlet.http.HttpServletRequest;
    12 
    13 import net.sf.json.JSONObject;
    14 
    15 import org.springframework.web.bind.annotation.RequestMapping;
    16 import org.springframework.web.bind.annotation.RequestMethod;
    17 import org.springframework.web.bind.annotation.ResponseBody;
    18 
    19 public class Test {
    20 
    21     //调用http接口
    22     @RequestMapping(value="testInterfaces.htm", method={RequestMethod.GET, RequestMethod.POST})
    23     public void testInterfaces(HttpServletRequest request) throws Exception{
    24         //传入中文参数并设置编码格式
    25         String param = "{"url":"中文"}";
    26         param = URLEncoder.encode(param, "UTF-8");
    27             PrintWriter out = null;
    28             BufferedReader in = null;
    29             String result = "";
    30             try {
    31                URL realUrl = new URL("http://localhost/test.htm");
    32                // 打开和URL之间的连接
    33                URLConnection conn = realUrl.openConnection();
    34                // 发送POST请求必须设置如下两行
    35                conn.setDoOutput(true);
    36                conn.setDoInput(true);
    37                // 获取URLConnection对象对应的输出流
    38                out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
    39                // 发送请求参数
    40                out.print(param);          
    41                // flush输出流的缓冲
    42                out.flush();
    43                // 定义BufferedReader输入流来读取URL的响应
    44                in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
    45                String line;
    46                while ((line = in.readLine()) != null) {
    47                    result += line;
    48                }
    49                //解析json对象
    50                JSONObject jsStr = JSONObject.fromObject(result);
    51                System.out.println(jsStr.get("firstName"));
    52             } catch (Exception e) {
    53                e.printStackTrace();
    54             }
    55     }
    56 
    57     //所调用的接口
    58     @RequestMapping(value = "test.htm", method = { RequestMethod.GET,RequestMethod.POST })
    59     @ResponseBody
    60     public JSONObject test(HttpServletRequest request)throws Exception {       
    61         JSONObject jsonObj = new JSONObject();
    62         Map<String,Object> map = new HashMap<String,Object>();
    63         map.put("firstName", "jack");
    64         jsonObj.putAll(map);
    65         return jsonObj;
    66     }
    67 
    68 }
  • 相关阅读:
    根据EsLint配置WebStorm格式化代码风格
    Vue中使用vant-UI实现移动端自定义省市区三级联动
    Vue中使用Element-UI实现表格跨页多选
    Vue中使用iview-UI实现切换Tab页网络请求优化
    Vue中使用iview-UI按需引入Select组件下拉框无法生效问题
    Vue中使用iview-UI表格样式修改和使用自定义模板数据渲染相关
    Vue中使用Element-UI表单验证相关问题及解决
    Vue 3.0 多页面项目之商家平台练习
    五 创建道路模型(2 道路的挖填方量计算及条件部件)
    五 创建道路模型(1 道路三要素)
  • 原文地址:https://www.cnblogs.com/xiaoyue1606bj/p/11577266.html
Copyright © 2011-2022 走看看