本文實(shí)例講述了java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
本篇博客試用于編寫(xiě)java后臺(tái)接口以及兩個(gè)項(xiàng)目之間的接口對(duì)接功能;
具體的內(nèi)容如下:
1.java后臺(tái)給指定接口發(fā)送json數(shù)據(jù)
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= "服務(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ù)
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( "進(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)、美化、格式化工具:https://tool.zzvips.com/t/jsonjiexi/
json在線格式化工具:https://tool.zzvips.com/t/jsonformat/
在線excel轉(zhuǎn)json工具:https://tool.zzvips.com/t/excel2json/
json轉(zhuǎn)excel工具:https://tool.zzvips.com/t/json2excel/
在線json壓縮/轉(zhuǎn)義工具:https://tool.zzvips.com/t/jsonzip/
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/huxiangen/article/details/80433320