AFNetworking POST永远不会成功 - 不同设备上出现意外行为

时间:2012-12-03 23:33:40

标签: ios post afnetworking hang

我目前正在开发一款iOS应用,允许用户发布分类列表供其他用户查看。在所有设备上,该应用程序挂起“O%已上传”SVProgressHUD一段时间,然后显示“我们的服务器暂时无法使用。请稍后重试。”,这是我在状态情况下编码的内容来自服务器端的代码500错误。但是,在iOS模拟器上,一切都运行顺畅。

除此之外,涉及网络请求的所有其他功能都运行良好,因此它必须与实际上传过程有关。我在下面发布了相应的代码;如果您需要任何其他帮助来解决这个问题,请告诉我。

网络代码

- (IBAction)publishPressed:(UIBarButtonItem *)sender {
    [SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeBlack];

    // Set the params for the API call in an NSMutableDictionary called mutableParams.

    // Create the form request with the post parameters and image data to pass to the API.
    NSMutableURLRequest *request = [[UAPIClient sharedClient] multipartFormRequestWithMethod:@"POST" 
                                                              path:@"mobPost.php" 
                                                              parameters:mutableParams 
                                                              constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (self.imageHasBeenSet) {
            [self.listingImageView.image fixOrientation];
            NSData *imageData = UIImagePNGRepresentation(self.listingImageView.image);
            [formData appendPartWithFileData:imageData name:@"userfile" 
                      fileName:@"postImage.png" mimeType:@"image/png"]; 
        }
    }];

    // Create the `AFJSONRequestOperation` from the form request, with appropriate success and failure blocks.
    AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseJSON) {
        if ([[responseJSON objectForKey:@"status"] intValue] == 1) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showSuccessWithStatus:@"Success!"];
            });
            // Pass a message back to the delegate so that the modal view controller
            // can be dismissed successfully.
            [self.delegate uCreateListingTableViewController:self didCreateListing:YES];
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:@"Post failed. Please try again."];
            });
            NSLog(@"Status %@", [responseJSON objectForKey:@"status"]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"Our server is temporarily unavailable. Please try again later."];
        });
    }];

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showSuccessWithStatus:[[NSString stringWithFormat:@"%lli", (totalBytesWritten / totalBytesExpectedToWrite)] stringByAppendingString:@"% Uploaded"]];
        });
    }];

    [[UAPIClient sharedClient] enqueueHTTPRequestOperation:operation];
}

UAPIClient.m

#import "UAPIClient.h"
#import "AFJSONRequestOperation.h"

static NSString * const kUAPIBaseURLString = @"hiding string for privacy";

@implementation UAPIClient

+ (UAPIClient *)sharedClient {
    static UAPIClient *_sharedClient;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[UAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kUAPIBaseURLString]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

        // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }

    return self;
}

@end

0 个答案:

没有答案