锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. java实现后台发送及接收json数据的方法示例

java实现后台发送及接收json数据的方法示例

0
  • 软件开发
  • 发布于 2024-08-19
  • 0 次阅读
黄健
黄健

具体的内容如下:

1.java后台给指定接口发送json数据

|-------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package com.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import net.sf.json.JSONObject; public class testOne { ``public static void main(String[] args) { ``JSONObject jsobj1 = ``new JSONObject(); ``JSONObject jsobj2 = ``new JSONObject(); ``jsobj2.put(``"deviceID"``, ``"112"``); ``jsobj2.put(``"channel"``, ``"channel"``); ``jsobj2.put(``"state"``, ``"0"``); ``jsobj1.put(``"item"``, jsobj2); ``jsobj1.put(``"requestCommand"``, ``"control"``); ``post(jsobj1,``"http://192.168.3.4:8080/HSDC/test/authentication"``); ``} ``public static String post(JSONObject json,String path) { ``String result=``""``; ``try { ``HttpClient client=``new DefaultHttpClient(); ``HttpPost post=``new HttpPost(url); ``post.setHeader(``"Content-Type"``, ``"appliction/json"``); ``post.addHeader(``"Authorization"``, ``"Basic YWRtaW46"``); ``StringEntity s=``new StringEntity(json.toString(), ``"utf-8"``); ``s.setContentEncoding(``new BasicHeader(HTTP.CONTENT_TYPE, ``"appliction/json"``)); ``post.setEntity(s); ``HttpResponse httpResponse=client.execute(post); ``InputStream in=httpResponse.getEntity().getContent(); ``BufferedReader br=``new BufferedReader(``new InputStreamReader(in, ``"utf-8"``)); ``StringBuilder strber=``new StringBuilder(); ``String line=``null``; ``while ((line=br.readLine())!=``null``) { ``strber.append(line+``"\n"``); ``} ``in.close(); ``result=strber.toString(); ``if``(httpResponse.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){ ``result=``"服务器异常"``; ``} ``} ``catch (Exception e) { ``System.out.println(``"请求异常"``); ``throw new RuntimeException(e); ``} ``System.out.println(``"result=="``+result); ``return result; ``} } |

2.java后台接收json数据

|-------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.controller; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @RestController @RequestMapping``(``"test"``) public class TestConttroller{ ``@Resource ``protected HttpServletRequest request; ``@RequestMapping``(value=``"authentication"``,produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST) ``public Map<String,Object> getString() ``throws UnsupportedEncodingException, IOException{ ``System.out.println(``"进入====================="``); ``//后台接收 ``InputStreamReader reader=``new InputStreamReader(request.getInputStream(),``"UTF-8"``); ``char [] buff=``new char``[``1024``]; ``int length=``0``; ``while``((length=reader.read(buff))!=-``1``){ ``String x=``new String(buff,``0``,length); ``System.out.println(x); ``} ``//响应 ``Map<String,Object> jsonObject = ``new HashMap<String, Object>(); ``//创建Json对象 ``jsonObject.put(``"username"``, ``"张三"``); ``//设置Json对象的属性 ``jsonObject.put(``"password"``, ``"123456"``); ``return jsonObject; ``} } |

运行testOne之后将json数据发送到authentication接口,接收的数据如图:

testOne中main方法返回的数据如图:

原文链接: https://blog.csdn.net/cuiqwei/article/details/118548582

标签: #JAVA 991 #软件开发 1171
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.