AFNetworking how to POST a JSON data to the server

Source: Internet
Author: User

NSString * pathStr = @ "http://www.baidu.com ";

AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL: [NSURL URLWithString: @ ""]; // set the url to null

HttpClient. parameterEncoding = AFJSONParameterEncoding;

[HttpClient setDefaultHeader: @ "Accept" value: @ "text/json"];


NSMutableDictionary * params = [[NSMutableDictionary alloc] init]; // The json data to be passed is a dictionary.

[Params setObject: UserID forKey: @ "userId"];

[Params setObject: [self. contDic objectForKey: @ "weiboId"] forKey: @ "operateId"];

[Params setObject: @ "8d0ce196d0eafa27780cc3a7217de71d92474021dc0a4fc593d4f9b9eda320ca" forKey: @ "key"];

 

// HttpClient's postPath is the pathStr above, that is, the URL you want to access. Here, a data request is submitted to the server,

[HttpClient postPath: pathStr parameters: params success: ^ (AFHTTPRequestOperation * operation, id responseObject ){

NSLog (@ "data ===%@", params );

NSString * responseStr = [[NSString alloc] initWithData: responseObject encoding: NSUTF8StringEncoding];

NSLog (@ "Request Successful, response '% @'", responseStr );

[ResponseStr release];

} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){

NSLog (@ "[HTTPClient Error]: % @", error );

 

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.