您好,登錄后才能下訂單哦!
這篇文章主要介紹Java怎么實現后臺發送及接收json數據,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體的內容如下:
1.java后臺給指定接口發送json數據
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數據
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方法返回的數據如圖:
以上是“Java怎么實現后臺發送及接收json數據”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。