天天看點

java把接收的json_Java實作背景發送及接收json資料的方法示例

本文執行個體講述了java實作背景發送及接收json資料的方法。分享給大家供大家參考,具體如下:

本篇部落格試用于編寫java背景接口以及兩個項目之間的接口對接功能;

具體的内容如下:

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 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 jsonobject = new hashmap(); //建立json對象

jsonobject.put("username", "張三"); //設定json對象的屬性

jsonobject.put("password", "123456");

return jsonobject;

}

}

運作testone之後将json資料發送到authentication接口,接收的資料如圖:

java把接收的json_Java實作背景發送及接收json資料的方法示例

testone中main方法傳回的資料如圖:

java把接收的json_Java實作背景發送及接收json資料的方法示例

至此java背景發送及接收json資料代碼也就完成了

ps:關于json操作,這裡再為大家推薦幾款比較實用的json線上工具供大家參考使用:

線上json代碼檢驗、檢驗、美化、格式化工具:

json線上格式化工具:

線上xml/json互相轉換工具:

json代碼線上格式化/美化/壓縮/編輯/轉換工具:

線上json壓縮/轉義工具:

希望本文所述對大家java程式設計有所幫助。

希望與廣大網友互動??

點此進行留言吧!