First, send JSON data to the server
Steps to send JSON data to the server:
(1) Be sure to use the POST request
(2) Set the request header
(3) Set the JSON data as the request body
code example:
1 #import "YYViewController.h"2 3 @interfaceYyviewcontroller ()4 5 @end6 7 @implementationYyviewcontroller8 9- (void) ViewdidloadTen { One [Super Viewdidload]; A } - -- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event the { - //1. Create a request -Nsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/order"]; -Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; +Request. HttpMethod =@"POST"; - + //2. Set the request header A[Request SetValue:@"Application/json"Forhttpheaderfield:@"Content-type"]; at - //3. set the request body -Nsdictionary *json = @{ - @"order_id":@"123", - @"user_id":@"789", - @" Shop":@"Toll" in }; - to //NSData-Nsdictionary + //nsdictionary-NSData -NSData *data =[nsjsonserialization Datawithjsonobject:json options:nsjsonwritingprettyprinted Error:nil]; theRequest. Httpbody =data; * $ //4. Sending the requestPanax Notoginseng [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) { -NSLog (@"%d", data.length); the }]; + } A the @end
Two, multi-value parameters
Multivalued parameter: One parameter corresponds to multiple values.
Request parameters such as the following:
Http://192.168.1.103:8080/MJServer/weather?place= Beijing &place= Henan &place= Hunan
The place property of the server is an array. Therefore, using the same parameter does not overwrite the value of the server.
iOS Development Network-send JSON data to server and multivalued parameters