HttpClient 发送Json请求,结果返回Json

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
public static JSONObject post(String url,JSONObject json){  HttpClient client = new DefaultHttpClient();  HttpPost post = new HttpPost(url);  JSONObject response = null;  try {   StringEntity s = new StringEntity(json.toString());   s.setContentEncoding("UTF-8");   s.setContentType("application/json");   post.setEntity(s);      HttpResponse res = client.execute(post);   if(res.getStatusLine().getStatusCode() == HttpStatus.OK.value()){    HttpEntity entity = res.getEntity();    String charset = EntityUtils.getContentCharSet(entity);    response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset)));   }  } catch (Exception e) {   throw new RuntimeException(e);  }  return response; }
  1.   
 
 

如果你的内容没有嵌套关系,就全都是ojb.put(key, value);

如果有嵌套关系,就是 subObj=new JSONObject();obj.put(key, subObj);

如果需要数组,就用JSONArray 对象。


那么你需要的是:{header:{"code": -100, "message": {"title": "", "detail": ""}},body:{dataStores:{},parameters:{"args": ["menu_jw_pj_xspj_x", ">>"], "responseParam": "tooltip"}}}

这里面是有嵌套关系的,你先将其格式化,然后就知道嵌套关系了:

JavaScript code
?
1
2
3
4
5
6
7
8
9
10
11
{
     header : {
         "code"  : -100,  "message"  : {
             "title"  "" "detail"  ""
         }
     }, body : {
         dataStores : {}, parameters : {
             "args"  : [  "menu_jw_pj_xspj_x" ">>"  ],  "responseParam"  "tooltip"
         }
     }
};

注意方括号代表数组。

以前面为例:
JSONObject obj = new JSONObject();

JSONObject objHead = new JSONObject();
obj.put("head", objHead);
objHead.put("code", -100);

JSONObject objMsg = new JSONObject();
objHead.put("message", objMsg);
objMsg.put("title", "");
objMsg.put("detail", "");

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值