AFHTTPSessionManager问题

时间:2016-04-05 05:03:58

标签: ios objective-c iphone ios9 afnetworking-2

我正在使用Afnetworking Framework,但每次点击Web服务时,响应总是如下 -

错误Domain = com.alamofire.error.serialization.response Code = -1011“请求失败:未授权(401)”UserInfo = {com.alamofire.serialization.response.error.response

我使用下面的代码任何人都可以帮助我 -

 NSString * finalUlr =  [kMainUrl stringByAppendingString:url];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"1f76f057-1809-0bfd-5b53-f2f6c2d3ab81" forHTTPHeaderField:@"postman-token"];
[manager.requestSerializer setValue:@"qW9Bf_-RxXFz8mtxDqUYYzBN2V9N6he_tOTbSgJskVA" forHTTPHeaderField:@"x-csrf-token"];
[manager POST:finalUlr parameters:Patameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: Success");
    completionBlock(YES,responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    completionBlock(NO,nil);
}];

2 个答案:

答案 0 :(得分:-1)

试试这个

NSString * finalUlr =  [kMainUrl stringByAppendingString:url];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:finalUlr] cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

    [request setHTTPMethod:GET_METHOD]; // or POST_METHOD
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"1f76f057-1809-0bfd-5b53-f2f6c2d3ab81" forHTTPHeaderField:@"postman-token"];
    [request setValue:@"qW9Bf_-RxXFz8mtxDqUYYzBN2V9N6he_tOTbSgJskVA" forHTTPHeaderField:@"x-csrf-token"];

    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         if (responseObject)
         {

         }
         else
         {
         }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error){
     }];

    [op start];

希望这会有所帮助

答案 1 :(得分:-1)

希望它对你有用..

// stream() changed to Stream.of( ... ) as per library specs
mGeofenceList.addAll(Stream.of(GeofenceUtils.GeofenceObjects.entrySet())
                .map(entry -> new Geofence.Builder()
                .setRequestId(entry.getKey())
                .setCircularRegion(
                        entry.getValue().latitude,
                        entry.getValue().longitude,
                        GeofenceUtils.GEOFENCE_RADIUS_IN_METERS)
               .setExpirationDuration(GeofenceUtils.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
               .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)

               // Collectors works without prefix
               .build()).collect(Collectors.toList()));

注意: -

1)在str6中使用你的网址。

2)在参数中使用您的参数。

你可以传递这样的参数......

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];


[manager POST:str6 parameters:@{} progress:nil success:^(NSURLSessionTask *task , id responseObject)
 {

     NSLog(@"%@",responseObject);

 }failure:^(NSURLSessionTask *operation ,NSError *error)
 {

 }];

这里strEmail是你的价值等等。

@"电子邮件"是你的关键等等..