当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现后台发送及接收json数据的方法示例

Java实现后台发送及接收json数据的方法示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

新湖名苑,清幽异梦,赤麂

本文实例讲述了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<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数据代码也就完成了

ps:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线json代码检验、检验、美化、格式化工具:

json在线格式化工具:

在线xml/json互相转换工具:

json代码在线格式化/美化/压缩/编辑/转换工具:

在线json压缩/转义工具:

更多关于java相关内容感兴趣的读者可查看本站专题:《java操作json格式数据技巧总结》、《java数组操作技巧总结》、《java字符与字符串操作技巧总结》、《java数学运算技巧总结》、《java数据结构与算法教程》及《java操作dom节点技巧总结

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网