AFNetworking代码块未被执行

时间:2015-12-04 09:50:07

标签: php ios objective-c iphone afnetworking

我知道本网站不允许提出一般性问题 - 但我遇到的问题似乎与我的代码无关。这是我的代码工作一分钟而不工作的计算机问题之一 - 而且我没有改变任何东西。我将发布我认为相关的代码 - 但我没有收到任何错误消息,无论是在XCode的调试控制台中,还是在服务器端,apache2中错误日志。

可能相关的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImageWriteToSavedPhotosAlbum(info[UIImagePickerControllerOriginalImage], nil, nil, nil);

    NSString * uploadURL = @"http://192.168.1.4/upload.php";
    NSLog(@"uploadImageURL: %@", uploadURL);
    NSData *imageData = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage], 0.5);

    NSString *queryStringss = [NSString stringWithFormat:@"%@",uploadURL];
    queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //manager.responseSerializer.acceptableContentTypes = nil;
    manager.responseSerializer=[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];

    NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];

    NSString *usernameEncoded = [[userdefaults objectForKey:@"pfuser"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *params = @{@"username": usernameEncoded, @"count": [userdefaults valueForKey:@"count"]};

    [manager POST:queryStringss parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"file" fileName:@"file.jpeg" mimeType:@"image/jpeg"];
        }
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
              NSInteger count = [userdefaults integerForKey:@"count"];
              NSNumber *stored = [[NSNumber alloc] initWithInt:count];
              count++;
              [userdefaults setInteger:count forKey:@"count"];


              GMSMarker *marker3 = [[GMSMarker alloc] init];
              marker3.position = mapView_.myLocation.coordinate;
              marker3.title = [userdefaults objectForKey:@"pfuser"];
              marker3.map = mapView_;

              PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:mapView_.myLocation.coordinate.latitude longitude:mapView_.myLocation.coordinate.longitude];

              PFObject *pointstore = [PFObject objectWithClassName:@"MapPoints"];
              pointstore[@"title"] = [userdefaults objectForKey:@"pfuser"];
              pointstore[@"location"] = point;
              pointstore[@"count"] = stored;

              [pointstore saveEventually:^(BOOL succeeded, NSError *error) {

                  if (error) {
                      NSLog(@"Error");
                  }
                  else {
                      dispatch_async(dispatch_get_main_queue(), ^(void){
                          mapView_.settings.myLocationButton = YES;
                      });
                  }
              }];
          }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@ ***** %@", operation.responseString, error);
          }];

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

以上是应该执行的代码。它似乎在[manager POST:queryStringss parameters:params...

之前执行所有操作

我没有看到NSLog成功或失败的消息,它只是拖延了......虽然应用程序没有崩溃......应用程序继续正常工作。

今天早上将非常感谢任何帮助(我愿意与某人一起工作,他们知道他们在做什么,在聊天或类似的事情中)。提前谢谢!

更新

PHP服务器端代码:

<?php

header("Content-Type: application/json");

$username = $_POST['username'];
$count = $_POST['count'];
$base64string = base64_encode(file_get_contents("images/".$username."/".$count."/".$username."file".$count.".jpeg"));
echo json_encode(array("image" => $base64string));

?>

更新

这是我现在声明manager的地方:

#import "ViewController.h"
#import "SignUpController.h"
#import <Parse/Parse.h>
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
@import GoogleMaps;

typedef void (^CompletionHandlerType)();

@interface ViewController ()

@end


@implementation ViewController {
    GMSMapView *mapView_;
    BOOL firstLocationUpdate_;
    CLLocationManager *locationManager;
    UIImageView *image;
    UIButton *respondButton;
    AFHTTPRequestOperationManager *manager;
}

...

更新

在TheEye的一些精彩帮助之后 - 我们发现,邮差(镀铬扩展),由于某种原因,应用程序只是找不到页面......我得到了404响应(来自邮递员) 。我仍然无法理解为什么会发生这种情况,我们将非常感激帮助...但我可能不会马上回应,因为我很累。

2 个答案:

答案 0 :(得分:0)

愚蠢的防火墙必须已经开始随机阻止内容。我禁用了防火墙,一切都重新开始了。

答案 1 :(得分:-1)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];

UIImage *fetchedImage = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];

 NSData *imageData = UIImageJPEGRepresentation(fetchedImage,0.0);

 [self SetUserProfilePhotoData:imageData];

}


-(void)SetUserProfilePhotoData:(NSData*)imageData
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer.timeoutInterval = 15000000000000;
manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
// [serializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;





NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];

NSString *usernameEncoded = [[userdefaults objectForKey:@"pfuser"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSDictionary *params = @{@"username": usernameEncoded, @"count": [userdefaults valueForKey:@"count"]};

NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:@“http://192.168.1.4/upload.php” parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //  [formData appendPartWithFormData:imageData name:@"image"];


    [formData appendPartWithFileData:imageData name:@"image" fileName:"file.jpeg" mimeType:@"application/octet-stream"];


} error:nil];


AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
    success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                      dispatch_async(dispatch_get_main_queue(), ^{
                                     [SVProgressHUD showSuccessWithStatus:@"Uploaded!"];
                                      });                                        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
                              NSLog(@"Success %@", string);



                                     [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"UserProfilePic"];

                                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                     [SVProgressHUD showErrorWithStatus:@"Error in uploading!"];
                                     NSLog(@"Failure %@", error.description);
                                 }];

[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                    long long totalBytesWritten,
                                    long long totalBytesExpectedToWrite) {


   // NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);

    float progress =  (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
     [SVProgressHUD showProgress:progress status:[NSString stringWithFormat:@"Uploading %@%%",[NSString stringWithFormat:@"%.0f", progress*100]] maskType:SVProgressHUDMaskTypeClear];

}];

// 5. Begin!
[operation start];


}