通过JSON的形式请求后台数据

直接上代码
1、方式1

// 方式1
- (void)test1
{
    
    //传入的参数
    NSDictionary *parameterDic = @{@"username":@"18514531833",@"password":@"asdasd",@"code":@"9999"};
    //你的接口地址
    NSString *url=@"http://sanbox.api.xianjindai.rongba.com:8000/passport/login";
    
    
    
    AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
    
    
    NSMutableURLRequest *request = [serializer requestWithMethod:@"POST" URLString:url parameters:parameterDic error:nil];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                                   NSJSONSerialization *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                   NSDictionary *dicjson = (NSDictionary *)json;
                                   NSLog(@"%@",dicjson);
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                       //刷新UI
                                   });
                                   
                               }
                           }];
}

2、方式2,使用AFNetWorking

// 方式2,使用AFNetWorking
- (void)test2
{
    
    //传入的参数
    NSDictionary *parameterDic = @{@"username":@"18514531833",@"password":@"asdasd",@"code":@"9999"};
    //你的接口地址
    NSString *url = @"http://sanbox.api.xianjindai.rongba.com:8000/passport/login";

    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:error];
        NSString *argString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return argString;
    }];
    
    [manager POST:url parameters:parameterDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"responseObject ==== %@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error ==== %@",error.description);
        
    }];
}

Demo参考地址:https://github.com/RunOfTheSnail/JSONDataDemo

你可能感兴趣的:(通过JSON的形式请求后台数据)