2012-03-17 108 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); 
}];