国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

如何POST一個(gè)JSON格式的數(shù)據(jù)給Restful服務(wù)

2019-11-14 14:47:08
字體:
供稿:網(wǎng)友

在Android/java平臺(tái)上實(shí)現(xiàn)POST一個(gè)json數(shù)據(jù):

JSONObject jsonObj = new JSONObject();jsonObj.put("username", username);jsonObj.put("apikey", apikey);// Create the POST object and add the parametersHttpPost 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實(shí)現(xiàn):

 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);

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 遂川县| 内丘县| 静乐县| 会昌县| 开原市| 石景山区| 昌邑市| 洛阳市| 江门市| 夏邑县| 旺苍县| 馆陶县| 永新县| 峨眉山市| 互助| 瑞丽市| 侯马市| 绥中县| 井冈山市| 灵川县| 高台县| 东阳市| 界首市| 蒙山县| 广水市| 南雄市| 上思县| 东乌珠穆沁旗| 丰台区| 达拉特旗| 西贡区| 大埔县| 图们市| 星座| 定西市| 张家川| 宁城县| 土默特右旗| 昌图县| 兴安盟| 荔波县|