激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法示例

Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法示例

2021-06-22 12:53huxiangen Java教程

這篇文章主要介紹了Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了java針對(duì)json格式數(shù)據(jù)的傳輸與操作相關(guān)技巧,需要的朋友可以參考下

本文實(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ù)如圖:

Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(shù)據(jù)的方法示例

testone中main方法返回的數(shù)據(jù)如圖:

Java實(shí)現(xiàn)后臺(tái)發(fā)送及接收json數(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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 精品成人av一区二区三区 | 日韩在线观看视频一区 | 国产欧美一区二区三区免费看 | 毛片在线免费观看完整版 | 成人免费看片a | 福利四区| 久久9久久 | 久久精品99北条麻妃 | 成人情欲视频在线看免费 | 九九热免费在线观看 | 国产精品99久久久久久久 | 爽毛片 | 成人爱爱电影 | 九九热免费视频在线观看 | 国产精品免费成人 | 久久国产精品区 | 欧洲精品色 | 国产中出在线观看 | 成人在线国产 | 极品大长腿啪啪高潮露脸 | 99精品国产一区二区三区 | 天天操天天碰 | 久在线播放 | 中文字幕电影免费播放 | 逼片| 久久精品国产99久久6动漫亮点 | 中日无线码1区 | 免费香蕉成视频成人网 | 亚洲午夜免费 | 欧美日韩手机在线观看 | 男男羞羞视频网站国产 | 毛毛片在线看 | 色视频一区二区 | 欧美黑人伦理 | 日韩视频在线不卡 | 欧美性生活久久 | 久久久久亚洲视频 | 九九精品在线观看 | 国产影院在线观看 | 久草手机在线观看视频 | 在线观看免费污视频 |