<span id="Label3"></p>One or 2 major management objects<p style="margin-left: 90px"><p style="margin-left: 90px"><br><span style="background-color: #ffff99">1.AFHTTPRequestOperationManager</span><br>* Package for the <span style="color: #0000ff">nsurlconnection</span><br><br><span style="background-color: #ffff99">2.AFHTTPSessionManager</span><br>* Package for the <span style="color: #0000ff">nsurlsession</span></p></p><br>second, the specific use of Afhttprequestoperationmanager<p style="margin-left: 90px"><br><span style="background-color: #ffff99"><span style="background-color: #ffff99">1. Create a manager</span></span><br><span style="color: #0000ff"><span style="color: #0000ff">Afhttprequestoperationmanager</span></span>*mgr = [<span style="color: #0000ff"><span style="color: #0000ff">Afhttprequestoperationmanager</span></span>manager];<br><br><span style="background-color: #ffff99"><span style="background-color: #ffff99">2. Package Request Parameters</span></span><br><span style="color: #0000ff"><span style="color: #0000ff">nsmutabledictionary</span></span>*params = [<span style="color: #0000ff"><span style="color: #0000ff">nsmutabledictionary</span></span>dictionary];<br>params[@ "username"] = @ "hahaha";<br>params[@ "pwd"] = @ "123";<br><br><span style="background-color: #ffff99"><span style="background-color: #ffff99">3. Sending the request</span></span><br><span style="color: #0000ff"><span style="color: #0000ff">NSString</span></span>*url = @ "http://localhost:8080/MJServer/login";<br>[mgr<span style="color: #ff0000"><span style="color: #ff0000">POST</span></span>: URL Parameters:params<br>success:^ (<span style="color: #0000ff"><span style="color: #0000ff">afhttprequestoperation</span></span>*operation, ID Responseobject) {<br> <span style="color: #008000"><span style="color: #008000">//when The request succeeds, call this block</span></span><br>NSLog (@ "request successfully---%@", responseobject);<br>} failure:^ (<span style="color: #0000ff"><span style="color: #0000ff">afhttprequestoperation</span></span>*operation, Nserror *error) {<br><span style="color: #008000"><span style="color: #008000">//the call to call this block when the request fails</span></span><br>NSLog (@ "request failed");<br>}];<br><span style="color: #008000"><span style="color: #008000">//get Request</span></span><br>[mgr<span style="color: #ff0000"><span style="color: #ff0000">GET</span></span>: URL Parameters:params<br>success:^ (<span style="color: #0000ff"><span style="color: #0000ff">afhttprequestoperation</span></span>*operation, ID Responseobject) {<br> <span style="color: #008000"><span style="color: #008000">//when The request succeeds, call this block</span></span><br>NSLog (@ "request successfully---%@", responseobject);<br>} failure:^ (<span style="color: #0000ff"><span style="color: #0000ff">afhttprequestoperation</span></span>*operation, Nserror *error) {<br><span style="color: #008000"><span style="color: #008000">//the call to call this block when the request fails</span></span><br>NSLog (@ "request failed");<br>}];</p><br>third, the analysis of the server return data<p style="margin-left: 90px"><br><span style="background-color: #ffff99"><span style="background-color: #ffff99">1.AFN can automatically parse the data returned by the server</span></span><br>* The data returned by the server is parsed as JSON by default<br><br><span style="background-color: #ffff99"><span style="background-color: #ffff99">2. Set how the data returned by the server is parsed</span></span><br><span style="background-color: #99ccff"><span style="background-color: #99ccff">1> as JSON to parse (default Practice)</span></span><br>* Mgr.responseserializer = [<span style="color: #0000ff"><span style="color: #0000ff">Afjsonresponseserializer</span></span>serializer];<br>* The type of responseobject is Nsdictionary or Nsarray<br><br><span style="background-color: #99ccff"><span style="background-color: #99ccff">2> as XML to parse</span></span><br>* Mgr.responseserializer = [<span style="color: #0000ff"><span style="color: #0000ff">Afxmlparserresponseserializer</span></span>serializer];<br>* The type of Responseobject is Nsxmlparser<br><br><span style="background-color: #99ccff"><span style="background-color: #99ccff">3> Direct return to data</span></span><br><span style="color: #ff0000"><span style="color: #ff0000">* meaning: tell AFN not to parse the data returned by the server, keep the original</span></span><br>* Mgr.responseserializer = [<span style="color: #0000ff"><span style="color: #0000ff">Afhttpresponseserializer</span></span>serializer];<br><br><span style="background-color: #ffff99"><span style="background-color: #ffff99">3. Note</span></span><br>* The data returned by the server must be aligned with Responseserializer.<br><span style="background-color: #99ccff"><span style="background-color: #99ccff">The 1> server is returning JSON data</span></span><br>*<span style="color: #0000ff"><span style="color: #0000ff">Afjsonresponseserializer</span></span><br>*<span style="color: #0000ff"><span style="color: #0000ff">Afhttpresponseserializer</span></span><br><br><span style="background-color: #99ccff"><span style="background-color: #99ccff">The 2> server returns the XML data</span></span><br>*<span style="color: #0000ff"><span style="color: #0000ff">Afxmlparserresponseserializer</span></span><br><span style="background-color: #ffffff"><span style="background-color: #ffffff">* <span style="color: #0000ff">Afhttpresponseserializer</span> </span></span><br><br><span style="background-color: #99ccff"><span style="background-color: #99ccff">3> The server is returning additional data</span></span><br>*<span style="color: #0000ff"><span style="color: #0000ff">Afhttpresponseserializer</span></span><br><br></p><p><p>IOS AFN Request Summary</p></p></span>
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