• HttpURLConnection传JSON数据


        try {
                //创建连接 
                URL url = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestProperty("Content-Type", "application/json");
                connection.connect();
                // POST请求 
                DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                JSONObject obj = new JSONObject();
                String json = java.net.URLEncoder.encode(obj.toString(), "utf-8");
                out.writeBytes(json);
                out.flush();
                out.close();
                // 读取响应 
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = URLDecoder.decode(lines, "utf-8");
                    sb.append(lines);
                }
                System.out.println(sb);
                reader.close();
                // 断开连接 
                connection.disconnect(); 
            } catch (MalformedURLException e) { 
                e.printStackTrace(); 
            } catch (UnsupportedEncodingException e) { 
                e.printStackTrace(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
  • 相关阅读:
    U3D不同平台载入XML文件的方法——IOS MAC Android
    C++使用规范小记
    设置角色对象可见性
    编辑器菜单操作
    U3D资源动态加载异步方案探究
    Animation动画
    Unity3D失去焦点时继续渲染
    C#打开当前目录
    组件模式代码实践(C#版本)
    Unity3D批处理脚本
  • 原文地址:https://www.cnblogs.com/duelsol/p/5128271.html
Copyright © 2020-2023  润新知