iOS Development Network-send JSON data to server and multivalued parameters

Source: Internet
Author: User

iOS Development Network-send JSON data to server and multivalued parameters

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:

#import"YYViewController.h"@interface Yyviewcontroller () @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload];}- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{    //1. Create a requestNsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/order"]; Nsmutableurlrequest*request =[Nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod=@"POST"; //2. Set the request header[Request SetValue:@"Application/json"Forhttpheaderfield:@"Content-type"]; //3. Set the request bodyNsdictionary *json = @{                           @"order_id":@"123",                           @"user_id":@"789",                           @" Shop":@"Toll"                           }; //NSData-Nsdictionary//nsdictionary-NSDataNSData *data =[nsjsonserialization Datawithjsonobject:json options:nsjsonwritingprettyprinted Error:nil]; Request. Httpbody=data; //4. Sending the request[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {NSLog (@"%d", data.length); }];} @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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.