如何POST一个JSON格式的数据给Restful服务

来源:互联网 发布:原始传奇翅膀进阶数据 编辑:程序博客网 时间:2024/06/16 15:07

转: http://blog.csdn.net/iefreer/article/details/8280345


在Android/java平台上实现POST一个json数据:

<code><span class="typ">JSONObject</span><span class="pln"> jsonObj </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">JSONObject</span><span class="pun">();</span><span class="pln">jsonObj</span><span class="pun">.</span><span class="pln">put</span><span class="pun">(</span><span class="str">"username"</span><span class="pun">,</span><span class="pln"> username</span><span class="pun">);</span><span class="pln">jsonObj</span><span class="pun">.</span><span class="pln">put</span><span class="pun">(</span><span class="str">"apikey"</span><span class="pun">,</span><span class="pln"> apikey</span><span class="pun">);</span><span class="pln"></span><span class="com">// Create the POST object and add the parameters</span><span class="pln"></span><span class="typ">HttpPost</span><span class="pln"> httpPost </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">HttpPost</span><span class="pun">(</span><span class="pln">url</span><span class="pun">);</span><span class="pln"></span><span class="typ">StringEntity</span><span class="pln"> entity </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StringEntity</span><span class="pun">(</span><span class="pln">jsonObj</span><span class="pun">.</span><span class="pln">toString</span><span class="pun">(),</span><span class="pln"> HTTP</span><span class="pun">.</span><span class="pln">UTF_8</span><span class="pun">);</span><span class="pln">entity</span><span class="pun">.</span><span class="pln">setContentType</span><span class="pun">(</span><span class="str">"application/json"</span><span class="pun">);</span><span class="pln">httpPost</span><span class="pun">.</span><span class="pln">setEntity</span><span class="pun">(</span><span class="pln">entity</span><span class="pun">);</span><span class="pln"></span><span class="typ">HttpClient</span><span class="pln"> client </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">DefaultHttpClient</span><span class="pun">();</span><span class="pln"></span><span class="typ">HttpResponse</span><span class="pln"> response </span><span class="pun">=</span><span class="pln"> client</span><span class="pun">.</span><span class="pln">execute</span><span class="pun">(</span><span class="pln">httpPost</span><span class="pun">);</span></code>


用curl可执行如下命令:

curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://domain/apis/users.json


用jQuery:

<code><span class="pln">$</span><span class="pun">.</span><span class="pln">ajax</span><span class="pun">({</span><span class="pln">  url</span><span class="pun">:</span><span class="pln">url</span><span class="pun">,</span><span class="pln">  type</span><span class="pun">:</span><span class="str">"POST"</span><span class="pun">,</span><span class="pln">  data</span><span class="pun">:</span><span class="pln">data</span><span class="pun">,</span><span class="pln">  contentType</span><span class="pun">:</span><span class="str">"application/json; charset=utf-8"</span><span class="pun">,</span><span class="pln">  dataType</span><span class="pun">:</span><span class="str">"json"</span><span class="pun">,</span><span class="pln">  success</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(){</span><span class="pln">    </span><span class="pun">...</span><span class="pln">  </span><span class="pun">}</span><span class="pln"></span><span class="pun">})</span></code>

PHP用cUrl实现:

[php] view plaincopy
  1. $data = array("name" => "Hagrid""age" => "36");                                                                      
  2. $data_string = json_encode($data);       
  3. $ch = curl_init('http://api.local/rest/users');        
  4. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                            
  5. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  7. curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
  8.     'Content-Type: application/json',  
  9.     'Content-Length: ' . strlen($data_string))           
  10. );                                                                                                                     
  11. $result = curl_exec($ch);  


by iefreer

版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0
原创粉丝点击