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;
  }
}
  • 接收
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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值