如何post一个json格式的数据给restful服务,jsonrestful在android/java平台上实现post一个json数据:
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可执行如下命令:
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实现:
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);
http://www.bkjia.com/phpjc/1053805.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1053805.htmltecharticle如何post一个json格式的数据给restful服务,jsonrestful 在android/java平台上实现post一个json数据: jsonobject jsonobj = new jsonobject (); jsonobj . put ( use...