{"id":1,"date":"2016-07-08T02:21:02","date_gmt":"2016-07-07T18:21:02","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1"},"modified":"2021-02-10T12:29:12","modified_gmt":"2021-02-10T04:29:12","slug":"hello-world","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2016\/07\/08\/0221021.html","title":{"rendered":"AFNetworking3.0 \u81ea\u5b9a\u4e49Request\u8bf7\u6c42HttpBody\u683c\u5f0f"},"content":{"rendered":"<p><strong>1\u3001\u5148\u6765\u770b\u4e00\u4e2aAFNetworking\u7684\u65b9\u6cd5<\/strong><\/p>\n<pre lang=\"objc\" line=\"1\">- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString\n?????????????????????????????parameters:(nullable id)parameters\n???????????????????????????????progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress\n????????????????????????????????success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success\n????????????????????????????????failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;<\/pre>\n<p><strong>2\u3001\u8fd9\u662fAFNetwroking\u6700\u4e3a\u5e38\u7528\u7684\u4e00\u4e2aPOST\u8bf7\u6c42\u65b9\u6cd5<\/strong><br \/>\n\u8be5\u65b9\u6cd5\u4e2d\u7684\u8bf7\u6c42\u53c2\u6570parameters\u6211\u4eec\u4e00\u822c\u662f\u4f20\u5165\u4e00\u4e2aNSDictionary\u5bf9\u8c61\uff0c\u800c\u4e8b\u5b9e\u4e0aAFNetworking\u5728\u5185\u90e8\u4f1a\u5c06\u5176\u904d\u5386\u62fc\u88c5\u6210key1=value1&amp;key2=value2\u8fd9\u6837\u7684\u683c\u5f0f\u540e\u52a0\u5165\u5230NSRequest\u8bf7\u6c42\u5f53\u4e2d\u53bb\uff0c\u5177\u4f53\u4e0d\u53d1\u6563\u8bb2\u4e86\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2aPOST\u65b9\u6cd5\u5f80\u91cc\u9762\u4e00\u5c42\u4e00\u5c42\u53bb\u7834\u5f00\uff0c\u6700\u7ec8\u5728\u7ec4\u5efaNSURLRequest\u80fd\u627e\u5230\u4e00\u6bb5\u8fd9\u6837\u7684\u4ee3\u7801<!--more--><\/p>\n<pre lang=\"objc\" line=\"1\">NSString *query = nil;\nif (parameters) {\n????if (self.queryStringSerialization) {\n????????NSError *serializationError;\n????????query = self.queryStringSerialization(request, parameters, &serializationError);\n?\n????????if (serializationError) {\n????????????if (error) {\n????????????????*error = serializationError;\n????????????}\n?\n????????????return nil;\n????????}\n????} else {\n????????switch (self.queryStringSerializationStyle) {\n????????????case AFHTTPRequestQueryStringDefaultStyle:\n????????????????query = AFQueryStringFromParameters(parameters);\n????????????????break;\n????????}\n????}\n}<\/pre>\n<p><strong>3\u3001\u8fd9\u91cc\u7684\u5173\u952e\u662fif (self.queryStringSerialization)\u8fd9\u4e2a\u5224\u65ad<\/strong><br \/>\n\u901a\u8fc7\u65ad\u70b9\u53ef\u4ee5\u770b\u5230\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\u8fd9\u4e2aif\u6761\u4ef6\u6c38\u8fdc\u90fd\u662f\u4e0d\u6210\u7acb\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e2aself.queryStringSerialization\u6b63\u5e38\u60c5\u51b5\u4e0b\u4e3a\u7a7a\uff0cqueryStringSerialization\u662fAFHTTPRequestSerializer\u7c7b\u7684\u4e00\u4e2a\u6210\u5458\u5c5e\u6027\uff0c\u5e76\u4e14\u662f\u4e2ablock\u7c7b\u578b\u7684\u5c5e\u6027\uff01<\/p>\n<pre lang=\"objc\" line=\"1\">typedef NSString * (^AFQueryStringSerializationBlock)(NSURLRequest *request, id parameters, NSError *__autoreleasing *error);<\/pre>\n<p>\u8fd8\u662f\u4e00\u4e2a\u5e26\u8fd4\u56de\u503c\u7684block<\/p>\n<p><strong>4\u3001\u8bf4\u5230\u8fd9\u91cc\u624d\u662f\u91cd\u70b9<\/strong><br \/>\n\u8fd9\u4e2aif\u6761\u4ef6\u4e0d\u6210\u7acb\u4f1a\u8fdb\u5165\u4e0b\u9762\u90a3\u4e2aelse\u5757\uff0c\u8fd9\u4e2aelse\u5757\u5c31\u662f\u4e0a\u9762\u6240\u8bf4\u7684\u5c06\u6211\u4eec\u4f20\u5165\u7684\u53c2\u6570\u8f6c\u6210key=value\u683c\u5f0f\uff0c\u4f46\u5982\u679c\u6211\u4eec\u7684\u670d\u52a1\u7aef\u8981\u6c42\u53c2\u6570\u4ee5json\u4e32\u7684\u683c\u5f0f\u4e0a\u4f20\u600e\u4e48\u529e\u5462\uff1f\uff08\u968f\u7740json\u7684\u6d41\u884c\uff0c\u8fd9\u79cd\u505a\u6cd5\u73b0\u5728\u8d8a\u6765\u8d8a\u591a\u4e86\u54e6\uff09<\/p>\n<p><strong>5\u3001\u91cd\u70b9\u7684\u91cd\u70b9\u5c31\u662f\u8fd9\u4e2aqueryStringSerialization\u5c5e\u6027<\/strong><br \/>\n\u8fd9\u4e2ablock\u91cc\u9762\u63d0\u4f9b\u4e86\brequest\u548cparameters\u7136\u540e\u8ba9\u6211\u4eec\u8fd4\u56de\u4e00\u4e2aNSString\uff0c\u8fd9\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u610f\u601d\u5c31\u662f\u7f51\u7edc\u6846\u67b6\u9700\u8981\u7ec4\u88c5\u7684NSURLRequest\u8bf7\u6c42\u4ee5\u53caparameters\u90fd\u7ed9\u4f60\uff0c\u8ba9\u4f60\u5229\u7528\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u8fd4\u56de\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684HttpBody\u4f53\u7ed9\u6846\u67b6\uff0c\u660e\u767d\u4e86\u5427\u3002<\/p>\n<p><strong>6\u3001\u8fd9\u662f\u4e00\u79cd\u89c4\u5219\uff0c\u4e0d\u9700\u8981\u5728\u6bcf\u6b21\u8bf7\u6c42\u91cc\u9762\u5199\uff0c\u53ea\u9700\u8981\u5728\u521d\u59cb\u5316\u7f51\u7edc\u5f15\u64ce\u65f6\u6307\u5b9a\u5c31\u884c\u4e86\uff0c\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u4f9b\u53c2\u8003<\/strong>\uff1a<\/p>\n<pre lang=\"objc\" line=\"1\">[self.networkManager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id? _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {\n????\/\/ \u5c06NSDictionary\u5bf9\u8c61\u8f6c\u6210JSON\u5b57\u7b26\u4e32\n????return [NSString JSONStringWithObject:parameters];\n}];<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u6765\u770b\u4e00\u4e2aAFNetworking\u7684\u65b9\u6cd5 &#8211; (nullable NSURLSessionDataTask *)POST:(NSString *)URLString ?????????????????????????????parameters:(nullable id)parameters ???????????????????????????????progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress ????????????????????????????????success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success ????????????????????????????????failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure; 2\u3001\u8fd9\u662fAFNetwroking\u6700\u4e3a\u5e38\u7528\u7684\u4e00\u4e2aPOST\u8bf7\u6c42\u65b9\u6cd5 \u8be5\u65b9\u6cd5\u4e2d\u7684\u8bf7\u6c42\u53c2\u6570parameters\u6211\u4eec\u4e00\u822c\u662f\u4f20\u5165\u4e00\u4e2aNSDictionary\u5bf9\u8c61\uff0c\u800c\u4e8b\u5b9e\u4e0aAFNetworking\u5728\u5185\u90e8\u4f1a\u5c06\u5176\u904d\u5386\u62fc\u88c5\u6210key1=value1&amp;key2=value2\u8fd9\u6837\u7684\u683c\u5f0f\u540e\u52a0\u5165\u5230NSRequest\u8bf7\u6c42\u5f53\u4e2d\u53bb\uff0c\u5177\u4f53\u4e0d\u53d1\u6563\u8bb2\u4e86\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2aPOST\u65b9\u6cd5\u5f80\u91cc\u9762\u4e00\u5c42\u4e00\u5c42\u53bb\u7834\u5f00\uff0c\u6700\u7ec8\u5728\u7ec4\u5efaNSURLRequest\u80fd\u627e\u5230\u4e00\u6bb5\u8fd9\u6837\u7684\u4ee3\u7801<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[18],"class_list":["post-1","post","type-post","status-publish","format-standard","hentry","category-project","tag-network"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}