2012-03-17 104 views
0

我用ASIHTTPRequest工作:发送HTTP请求并解析JSON响应

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]; 

由于ASIHTTPRequest不再维持,我搬到AFNetworking API。 但是,当从一个逻辑移动到另一个不同时,我有点困惑,我想知道如何通过AFNetworking传递相同的请求。

Thanx提前。

+0

为什么不使用更新NSURLConnection的方法呢? – bandejapaisa 2012-03-17 21:39:37

+0

我需要使用'AFNetworking' API。 – Luca 2012-03-17 22:04:29

+0

[AFNetworking Post Request]的可能重复(http://stackoverflow.com/questions/7623275/afnetworking-post-request) – JosephH 2012-03-18 06:53:55

回答

-2

我刚刚从AFNetworking开始。经过一些摔跤(和一个愚蠢的错误),我能够得到POST例程下面的工作与一个用户名一起发布和图像。

有一件事 - 不知道是否需要 - 但我初始化并保留AFHTTPClient的实例。还要创建一个NSOperationQueue实例来管理请求。

无论如何 - 希望下面的代码是有用的。它为我工作。

//财产初始化viewDidLoad

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

//方法

- (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]; 

}

+0

问题是如何将该示例在ASI中翻译为AFN。这个代码示例是可以的(虽然'AFHTTPClient'已经有了自己的操作队列 - 你不需要自己创建),但是它并没有真正回答这个问题。 – mattt 2012-03-29 19:23:36

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); 
}];