-(Nsurlsessiondatatask *) Defaultrequestwithurl: (NSString *) URL withparameters: (nsdictionary *) Parameters WithMethod : (NSString *) method Withblock: (void (^) (nsdictionary *dict, Nserror *error)) block { Default printing of incoming arguments #ifdef DEBUG NSLog (@ "Common method =%@", method);//get or post NSLog (@ "Common URL =%@", url);/The requested URL NSLog (@ "Common parameters =%@", parameters);/Incoming arguments #endif
To determine whether a get or post method is invoked in Afnetworking according to the method string if ([Method isequaltostring:@ ' Get "]) {//is used in AFNetworking3.1.0, the new progress progress block is added return [[Afappdotnetapiclient sharedclient] Get:url parameters:parameters progress:^ (nsprogress * _Nonnull DownloadProgress) {
} success:^ (Nsurlsessiondatatask * _nonnull task, id _nullable JSON) { #ifdef DEBUG NSLog (@ "common get JSON =%@", JSON);//print acquired JSON #endif
Nsdictionary *dict = json;//directly back to the dictionary for easy use if (block) { Block (dict, nil); } } failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) { Returns an empty dictionary and Nserror pointer if an error is requested if (block) { Block ([Nsdictionary dictionary], error);//This is a bit of a pit, because, for example, I want to judge whether a request succeeds based on a field, and I have an empty dictionary @{}, According to a key fetch value:[@{} objectforkey:@ "ErrorCode"], Then determine if the string's Intvalue equals 0:[[@{} objectforkey:@ "ErrorCode"] intvalue] = = 0 is returned 1. I think the solution is to change from the server side, the return of the field key with Issuccess,value is the string true or false can be resolved, But this can only know that success or failure can not be based on ErrorCode code for other operations, because ErrorCode can be 0, 1, 2, 3, and so on. }
Determine if the error is null from the pointer level, and print the error if it is not empty if (Error) { NSLog (@ "%@", error); } }]; }
Post related code return [[Afappdotnetapiclient sharedclient]post:url parameters:parameters progress:^ (nsprogress * _Nonnull uploadprogress) {
} success:^ (Nsurlsessiondatatask * _nonnull task, id _nullable JSON) { #ifdef DEBUG NSLog (@ "common post json =%@", JSON);//print acquired JSON #endif Nsdictionary *dict = json;//directly back to the dictionary for easy use
if (block) { Block (dict, nil); } } failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) { Returns an empty dictionary and Nserror pointer if an error is requested if (block) { Block ([Nsdictionary dictionary], error); }
Determine if the error is null from the pointer level, and print the error if it is not empty if (Error) { NSLog (@ "%@", error); } }];
The return value is temporarily unavailable and does not need to create a variable to receive; The self pointer passed in is not used so this method is highly portable. } -(void) Request { Test Network Request method 1. The use of the Web site I have previously caught, the API is written in PHP, the server is very stable, I often used for testing, but also for testing 2.get parameter start and end pass unsigned integer Nsdictionary *parameters = @{ @ "type": @ "List", @ "City": @ "2", @ "lid": @ "31", @ "SortBy": @ "1", @ "Start": @ "0", @ "End": @ "3" };
[Self defaultrequestwithurl:@ "/api.php" withparameters:parameters withmethod:@ "Get" withblock:^ (NSDictionary *dict, Nserror *error) {
}]; } |