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