0

I use to work with ASIHTTPRequest:

NSURL *url = [NSURL URLWithString:@"http://data.mywebsite/api/views/INLINE/rows.json?method=index"];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.requestMethod = @"POST";    
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];

    [request setDelegate:self];
    [request setCompletionBlock:^{        
        NSString *responseString = [request responseString];
        NSLog(@"Response: %@", responseString);
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"Error: %@", error.localizedDescription);
    }];

    [request startAsynchronous];

Since ASIHTTPRequest is no longer maintained, i moved to AFNetworking API. However, it's a bit confusing when moving from a logic to another different, i want to know how to pass the same request with AFNetworking.

Thanx in advance.

Luca
  • 20,399
  • 18
  • 49
  • 70

2 Answers2

1
NSURL *url = [NSURL URLWithString:@"http://data.mywebsite/api"];   
AFHTTPClient *client = [[[AFHTTPClient alloc] initWithBaseURL:url];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client postPath:@"views/INLINE/rows.json?method=index"
      parameters:json
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
           NSLog(@"Response: %@", operation.responseString);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           NSLog(@"Error: %@", error);
}];
JLamkin
  • 721
  • 6
  • 17
mattt
  • 19,544
  • 7
  • 73
  • 84
-2

I just started with AFNetworking too. After a little wrestling (and a stupid mistake) I was able to get the POST routine below working to post and image along with a userName.

One thing - don't know if it is required - but I init and keep an instance of AFHTTPClient around. Also create a NSOperationQueue instance to manage the request.

Anyway - hope the code below is of some use. It's working for me.

// property initialized in viewDidLoad

 client = [[AFHTTPClient alloc ]initWithBaseURL: [NSURL URLWithString:@"http://example.com/test/"]];

// method

- (void)test
{

UIImage * img = [self.paintingView  snapUIImage];
NSData *imageData = UIImageJPEGRepresentation(img, .5);

NSMutableDictionary * lParameters = [NSMutableDictionary dictionary];
[lParameters setObject:@"binky" forKey:@"user"];

NSMutableURLRequest *myRequest = 
[client multipartFormRequestWithMethod:@"POST" 
                                    path:@"loader.php"
                            parameters:lParameters 
             constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                     [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"image.jpg" mimeType:@"images/jpg"];
                 }];

[myRequest setTimeoutInterval: 5];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

[operation setCompletionBlock:^{
    NSLog(@"%@", operation.responseString); //Lets us know the result including failures

}];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];

}

spring
  • 18,009
  • 15
  • 80
  • 160
  • The question was how to translate that example in ASI to AFN. This code sample is alright (although `AFHTTPClient` already has its own operation queue--you don't need to make your own), but it doesn't really answer the question. – mattt Mar 29 '12 at 19:23