最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

POST一個JSON格式的數(shù)據(jù)給Restful服務實例詳解

 更新時間:2017年04月07日 14:15:45   投稿:lqh  
這篇文章主要介紹了POST一個JSON格式的數(shù)據(jù)給Restful服務實例詳解的相關資料,需要的朋友可以參考下

在Android/Java平臺上實現(xiàn)POST一個json數(shù)據(jù):

JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("apikey", apikey);
// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);

用curl可執(zhí)行如下命令:

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

用jQuery:

$.ajax({
 url:url,
 type:"POST",
 data:data,
 contentType:"application/json; charset=utf-8",
 dataType:"json",
 success: function(){
  ...
 }
})

PHP用cUrl實現(xiàn):

$data = array("name" => "Hagrid", "age" => "36");                                   
$data_string = json_encode($data);    
$ch = curl_init('http://api.local/rest/users');    
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");              
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(          
  'Content-Type: application/json', 
  'Content-Length: ' . strlen($data_string))      
);                                                           
$result = curl_exec($ch); 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

最新評論

松阳县| 富平县| 芦山县| 曲沃县| 都昌县| 民权县| 景泰县| 武安市| 镶黄旗| 昌黎县| 托里县| 山阴县| 张家川| 盐山县| 靖远县| 丹巴县| 竹山县| 河池市| 盐源县| 罗源县| 宁蒗| 固原市| 从化市| 桂林市| 凭祥市| 江陵县| 棋牌| 常宁市| 宁国市| 晋宁县| 方正县| 朝阳区| 阳新县| 临猗县| 鱼台县| 旬阳县| 许昌县| 武邑县| 三明市| 新密市| 涿州市|