IOS application/json file upload, etc., iosjson

Source: Internet
Author: User

IOS application/json file upload, etc., iosjson

In the process of interacting with the sever background, sometimes, they need to be uploaded by iOS developers in the form of "application/json.

 

NSString * accessUrl = [NSString stringWithFormat: @ "% @/xxx", @ "https://www.xxxxx.com: xxxx"]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: accessUrl]; request. HTTPMethod = @ "POST"; // set the request header [request setValue: @ "application/json" forHTTPHeaderField: @ "Content-Type"]; // set the Request body NSMutableData * body = [NSMutableData data]; [body appendData: [jsonStr dataUsingEncoding: Response]; [request setHTTPBody: body]; response * urlResponse = nil; NSError * error = [[NSError alloc] init]; NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: & urlResponse error: & error]; NSString * result = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding]; if (result = nil) {NSLog (@ "json parsing failed! ");} Else {NSData * jsonData = [result dataUsingEncoding: encoding]; NSError * err; NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: jsonData options: Unknown error: & err]; if (err) {NSLog (@ "json parsing failed: % @", err);} success (dic );}

 

Related Article

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.