Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法示例
本文實(shí)例講述了Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
本篇博客試用于編寫java后臺(tái)接口以及兩個(gè)項(xiàng)目之間的接口對(duì)接功能;
具體的內(nèi)容如下:
1.java后臺(tái)給指定接口發(fā)送json數(shù)據(jù)
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="服務(wù)器異常"; } } catch (Exception e) { System.out.println("請(qǐng)求異常"); throw new RuntimeException(e); } System.out.println("result=="+result); return result; } }
2.java后臺(tái)接收json數(shù)據(jù)
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("進(jìn)入====================="); //后臺(tái)接收 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); } //響應(yīng) Map<String,Object> jsonObject = new HashMap<String, Object>(); //創(chuàng)建Json對(duì)象 jsonObject.put("username", "張三"); //設(shè)置Json對(duì)象的屬性 jsonObject.put("password", "123456"); return jsonObject; } }
運(yùn)行testOne之后將json數(shù)據(jù)發(fā)送到authentication接口,接收的數(shù)據(jù)如圖:
testOne中main方法返回的數(shù)據(jù)如圖:
至此java后臺(tái)發(fā)送及接收json數(shù)據(jù)代碼也就完成了
PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java操作json格式數(shù)據(jù)技巧總結(jié)》、《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring 靜態(tài)變量/構(gòu)造函數(shù)注入失敗的解決方案
我們經(jīng)常會(huì)遇到一下問題:Spring對(duì)靜態(tài)變量的注入為空、在構(gòu)造函數(shù)中使用Spring容器中的Bean對(duì)象,得到的結(jié)果為空。不要擔(dān)心,本文將為大家介紹如何解決這些問題,跟隨小編來(lái)看看吧2021-11-11Java Collections的emptyList、EMPTY_LIST詳解與使用說(shuō)明
這篇文章主要介紹了Java Collections的emptyList、EMPTY_LIST詳解與使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11spring cloud gateway 限流的實(shí)現(xiàn)與原理
這篇文章主要介紹了spring cloud gateway 限流的實(shí)現(xiàn)與原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Java中super關(guān)鍵字介紹以及super()的使用
這幾天看到類在繼承時(shí)會(huì)用到this和super,這里就做了一點(diǎn)總結(jié),下面這篇文章主要給大家介紹了關(guān)于Java中super關(guān)鍵字介紹以及super()使用的相關(guān)資料,需要的朋友可以參考下2022-01-01java開源調(diào)度如何給xxljob加k8s執(zhí)行器
這篇文章主要介紹了java開源調(diào)度如何給xxljob加一個(gè)k8s執(zhí)行器,?xxljob?在設(shè)計(jì)上,抽象出了執(zhí)行器的接口,所以實(shí)現(xiàn)一個(gè)語(yǔ)言的執(zhí)行器并不復(fù)雜,這里主要探索下,如何利用k8s的pod?的能力,使用?xxljob?調(diào)度?pod?運(yùn)行,實(shí)現(xiàn)一個(gè)通用的和語(yǔ)言無(wú)關(guān)的執(zhí)行器2022-02-02Java 實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音的實(shí)例
這篇文章主要介紹了Java 實(shí)現(xiàn)漢字轉(zhuǎn)換為拼音的實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12利用Java寫一個(gè)學(xué)生管理系統(tǒng)
今天這篇文章就給給大家分享利用Java寫一個(gè)學(xué)生管理系統(tǒng)吧,先寫一個(gè)簡(jiǎn)單的用List來(lái)實(shí)現(xiàn)學(xué)生管理系統(tǒng):2021-09-09java配置數(shù)據(jù)庫(kù)連接池的方法步驟
java配置數(shù)據(jù)庫(kù)連接池的方法步驟,需要的朋友可以參考一下2013-05-05