0

私は使用する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];

ASIHTTPRequest がメンテナンスされなくなったので、AFNetworkingAPI に移動しました。ただし、ロジックから別のロジックに移動するときは少し混乱します。同じリクエストを で渡す方法を知りたいですAFNetworking

事前にサンクス。

4

2 に答える 2

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);
}];
于 2012-03-29T19:21:06.647 に答える
-2

私も始めたばかりAFNetworkingです。少し格闘した後 (そして愚かな間違い)、以下の POST ルーチンを使用して、userName とともに投稿および画像を作成することができました。

1つ-必要かどうかはわかりませんが、初期化して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];

}

于 2012-03-17T21:58:39.913 に答える