1

我正在使用AFNetworking最新版本查询我的服务器。

我的服务器端 PHP 正在发送与 PHP 相关的错误,但我无法看到错误是什么(因此我可以调试它),因为 AFNetworking 需要 JSON。

谁能告诉我如何才能看到完整的 HTTP 结果。我已经研究过,我知道这与我当前块没有的/块operation.responseString内部有关,我无法以某种方式将它添加为另一个块变量。successfailAFHTTPRequestOperation* operation

我真的很感激一些帮助。

以下代码使用

NSURL *url = [[NSURL alloc]initWithString:URL];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];


    NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:URLPATH parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                             {
                                 //...
                                 }

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                                                                                            NSLog(@"Inside the success block %@",JSON);
                                                                                            [[self delegate ]shareExerciseDone:JSON];
                                                                                            [[self delegate] activityIndicatorFinish];

                                                                                        }
                                                                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){

                                                                                            NSLog(@"Response text is: %@", operation.responseString);
                                                                                            NSLog(@"json text is: %@", JSON);
                                                                                            NSLog(@"Request failed with error: %@, %@", error, error.userInfo);

                                                                                            [[self delegate] activityIndicatorFinish];
                                                                                        }];

    [operation start];

错误

Request failed with error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xc1b4520 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}, {
    NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";
4

3 回答 3

1

为了将图像从 iOS 上传到服务器,我正在使用 Afnetworking

你能试试这个代码吗?你会得到成功或失败的回应。

NSData *userImgToUpload = UIImagePNGRepresentation(userImg.image);
NSURL *url = [NSURL URLWithString:@"www.domainname.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSLog(@"%@",@"Uploading data");
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:usernameTxt.text,@"username", firstnameTxt.text,@"first_name",lastnameTxt.text,@"last_name", nil]; 

//        Upload Image
    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/addPatient" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:userImgToUpload name:@"patientimage" fileName:[NSString stringWithFormat:@"%@.png",usernameTxt.text] mimeType:@"image/png"];
    }];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"response: %@",[operation.responseString objectFromJSONString]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Upload Failed");
        NSLog(@"error: %@", [operation error]);
    }];
    [operation start];
于 2013-08-22T16:56:46.687 回答
1

不使用AFJSONRequestOperation,只使用AFHTTPRequestOperation

于 2013-08-22T16:57:55.553 回答
0

您应该监听在操作开始和结束时触发的AFNetworkingOperationDidStartNotificationAFNetworkingOperationDidFinishNotification事件:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(YourOperationStartMethod:) 
                                             name:AFNetworkingOperationDidStartNotification 
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(YourOperationFinishMethod:) 
                                             name:AFNetworkingOperationDidFinishNotification 
                                           object:nil];

您可以像这样从通知中获取操作对象:

AFHTTPRequestOperation *operation = 
        (AFHTTPRequestOperation *) [notification object];

operation对象包含requestresponse属性。

可以在AFHTTPRequestOperationLogger插件中找到所有这些工作原理的一个很好的示例(当然,您可以简单地使用它,而不是编写自己的东西)。

于 2014-08-12T21:26:54.803 回答