Note: GET request, request parameter placed on URL, POST request, request parameter suggestion in JSON format inside request body
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 @interface Yyviewcontroller () 4 5 @end 6 7 @implementation Yyviewcontroller 8 9- (void) ViewDidLoad10 {One-to-one [super viewdidload];12}13-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) even t15 {16//1. Create request Nsurl *url = [Nsurl urlwithstring:@ "Http://192.168.1.200:8080/MJServer/order"];18 nsmutabl Eurlrequest *request = [nsmutableurlrequest requestwithurl:url];19 request. HttpMethod = @ "POST"; 20 21//2. To set the request header "[Request setvalue:@" Application/json "forhttpheaderfield:@" Content-typ E "];23 24//3. Set Request body Nsdictionary *json = @{26 @" order_id ": @" 123 ", 27 @ "user_id": @ "789", @ "shop": @ "Toll" 29};30 //NSData-NSDictionary32//Nsdictionary-NSData33 nsdata *data = [Nsjsonserialization datawit Hjsonobject:json options:nsjsonwritingprettyprinted error:nil];34 Request. Httpbody = data;35 36//4. Send Request [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mai Nqueue] completionhandler:^ (nsurlresponse *response, NSData *data, Nserror *connectionerror) {NSLog (@ "%d", data . length);}];40}41 @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