2014-11-30 7 views
7

Il codice seguente consiste nell'invio di immagini tramite un'operazioneQueue. Le richieste vengono tutte attivate una alla volta correttamente, la risposta del server contiene il nome del file immagine di cui il client ha bisogno per accedere. Il problema è che non è previsto il reponseObject per il blocco di successo/fallimento JSON analizzato ma il tipo di NSInLineData mostrato nel debugger. Ora sospetto che il codice per costruire l'operazione da NSMutableURLRequest abbia causato il problema. Per favore aiuto.responseObject non è JSON ma NSInLineData per AFHTTPRequestOperationManager

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer = [AFJSONResponseSerializer serializer]; 

NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST"  
      URLString:podURLString parameters:nil 
      constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

      NSError *error; 
      BOOL success =[formData appendPartWithFileURL:imgURL name:@"images" fileName:img.path 
       mimeType:@"image/jpg" error:nil]; 
      if (!success) 
        NSLog(@"appendPartWithFileURL error: %@", error);} error:nil]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Image Success: %@", [responseObject description]); 

    NSString *imagePath = [response objectForKey:@"imageFileName"]; 

    [self.delegate networkManager:self didSubmitDeliveryImageForImageID:imagePath]; 


} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Image Error: %@", error); 
    NSLog(@"image error: %@", [operation.responseObject description]); 

NSString *imageFilePath = [operation.responseObject objectForKey:@"imageFileName"]; 
[self.delegate networkManager:self didFailSubmitDeliveryImageForImageID:imageFilePath]; 

}]; 
[manager.operationQueue addOperation:operation]; 
+1

rimozione di questa linea 'manager.responseSerializer = [AFJSONResponseSerializer serializzatore];' ha lavorato per me. – gabbler

risposta

2

Basta aggiungere questa riga di codice prima della AFHTTPRequestOperation blocco

**operation.responseSerializer = [AFJSONResponseSerializer serializer];** 
+0

tipo di contenuto di risposta dal server non è application/json. sua applciation/octet-stream quindi questo non funziona per me. Cosa fare ? – Adeel

6

Quando si ottiene la risposta come NSInLineData. Va bene adesso. Puoi scrivere sotto un'unica riga di codice per ottenere NSDictionary se supporta il formato json.

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObjec options:0 error:nil]; 
+0

non succede nel mio caso. quali possono essere gli altri motivi? – Adeel

+0

puoi condividere API Call per controllarlo. –

Problemi correlati