AFNetwork数据请求之POST与GET方式 AFHTTPRequestOperationManager

利用 AFNetwork 进行远程访问 的一些常用代码


pod 'AFNetworking'


请求一般分为 post 和 get ,当然按照标准还有delete,update 等方法

1,进行post 上传数据的代码


<p class="p1"><span class="s1">    NSDictionary</span><span class="s2"> * params =</span></p><p class="p2"><span class="s1">      </span><span class="s3">@{</span></p><p class="p3"><span class="s2">            </span><span class="s4">@"clientID"</span><span class="s2">: [</span><span class="s1">Tools</span><span class="s2"> </span><span class="s1">uniqueGlobalDeviceIdentifier</span><span class="s2">],</span></p><p class="p1"><span class="s2">            </span><span class="s4">@"longitude"</span><span class="s2">:[</span><span class="s1">NSNumber</span><span class="s2"> </span><span class="s1">numberWithDouble</span><span class="s2">:coordinate.</span><span class="s1">longitude</span><span class="s2">],</span></p><p class="p1"><span class="s2">            </span><span class="s4">@"latitude"</span><span class="s2">:[</span><span class="s1">NSNumber</span><span class="s2"> </span><span class="s1">numberWithDouble</span><span class="s2">:coordinate.</span><span class="s1">latitude</span><span class="s2">],</span></p><p class="p1"><span class="s2">            </span><span class="s4">@"altitude"</span><span class="s2">:[</span><span class="s1">NSNumber</span><span class="s2"> </span><span class="s1">numberWithDouble</span><span class="s2">:</span><span class="s5">self</span><span class="s2">.</span><span class="s6">currentLocation</span><span class="s2">.</span><span class="s1">altitude</span><span class="s2">],</span></p><p class="p1"><span class="s2">            </span><span class="s4">@"course"</span><span class="s2">:[</span><span class="s1">NSNumber</span><span class="s2"> </span><span class="s1">numberWithDouble</span><span class="s2">:</span><span class="s5">self</span><span class="s2">.</span><span class="s6">currentLocation</span><span class="s2">.</span><span class="s1">course</span><span class="s2">],</span></p><p class="p1"><span class="s2">            </span><span class="s4">@"speed"</span><span class="s2">:[</span><span class="s1">NSNumber</span><span class="s2"> </span><span class="s1">numberWithDouble</span><span class="s2">:</span><span class="s5">self</span><span class="s2">.</span><span class="s6">currentLocation</span><span class="s2">.</span><span class="s1">speed</span><span class="s2">],</span></p><p class="p2"><span class="s1">      </span><span class="s3">}</span><span class="s1">;</span></p>    //通过 AFNetworking 的POST 调用
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString * post_url =[[Tools getServerHost] stringByAppendingString:@"/log/location_log" ];
    DLog(@"准备发送的url:%@", post_url);
    [manager POST:post_url parameters:sendLocationInfo success:^(AFHTTPRequestOperation *operation, id responseObject) {
        DLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"Error: %@", error);
    }];


2. 对get返回的对象进行json 处理 

      

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString * post_url =[[Tools getServerHost] stringByAppendingString:@"/road_name_alias/road_name_alias.json" ];
    DLog(@"准备发送的url:%@", post_url);
    //设置请求头contenttypes信息
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    [manager GET:post_url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        DLog(@"JSON: %@", responseObject);
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        //系统自带JSON解析
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
        Tools * tools = [Tools sharedInstance];
        tools.roadNameAliasList = resultDic;


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"Error: %@", error);
        Tools * tools = [Tools  sharedInstance];
        tools.roadNameAliasList = nil;

    }];


你可能感兴趣的:(AFNetwork数据请求之POST与GET方式 AFHTTPRequestOperationManager)