2013-08-22 143 views
1

我正在使用最新版本的AFNetworking查詢我的服務器。AFNetworking獲取完整的http響應

我的服務器端PHP發送一個PHP相關的錯誤,但我無法看到錯誤是什麼(所以我可以調試它)AFNetworking期待一個JSON。

任何人都可以告訴我如何看到完整的HTTP結果。我已經研究過,並且我知道這是success/fail塊中的operation.responseString,我當前的塊沒有AFHTTPRequestOperation* 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."; 
+0

你在尋找'response.statusCode'還是'response.allHeaderFields'? [NSHTTPURLResponse文檔](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPURLResponse_Class/Reference/Reference.html) – danielbeard

+0

不,我需要完整的HTML文本輸出PHP。我想看看它失敗的地方以及發送的內容不是JSON –

回答

1

不要使用AFJSONRequestOperation,只需使用AFHTTPRequestOperation

+0

是的,或者使用像Charles Web調試代理這樣的代理。 –

1

對於上傳圖片從iOS系統服務器我使用Afnetworking

,你可以嘗試用此Code.and你會成功或失敗地得到迴應。

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]; 
0

你應該聽的是火在操作的開始和結束AFNetworkingOperationDidStartNotificationAFNetworkingOperationDidFinishNotification事件:

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

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

可以從這樣的通知抓住操作對象此operation對象包含requestresponse屬性。

AFHTTPRequestOperationLogger插件(當然,您可以簡單地使用它,而不是寫自己的東西)中可以找到所有這些工作的一個很好的例子。