国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 含山县| 顺义区| 满洲里市| 神农架林区| 宁海县| 镇江市| 兰溪市| 巴青县| 洛宁县| 通州区| 黄骅市| 滕州市| 湘潭市| 寿宁县| 邳州市| 光泽县| 文安县| 顺义区| 信阳市| 衡阳县| 吴忠市| 确山县| 东阳市| 永宁县| 平乐县| 武强县| 祁阳县| 保山市| 香港| 田林县| 彰化县| 苍南县| 根河市| 巩义市| 武清区| 洮南市| 搜索| 宜城市| 汉沽区| 桐城市| 莱西市|