2013-05-30 8 views
6

Sto cercando di caricare una foto su ImageShack usando il loro API:iOS - caricamento di foto con ImageShack JSON API

- (void)uploadImage2:(UIImage *)image 
{ 
    NSData *imageToUpload = UIImagePNGRepresentation(image); 

    if (imageToUpload) 
    { 
     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
     [parameters setObject:@"XXXX" forKey:@"key"]; 
     [parameters setObject:@"json" forKey:@"format"]; 
     //[parameters setObject:@"application/json" forKey:@"Content-Type"]; 

     AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]]; 

     NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"logo.png" mimeType:@"image/png"]; 
     }]; 

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       //NSLog(@"Upload Failed"); 
       return; 
      } 
      //NSLog(@"error: %@", [operation error]); 

     }]; 

     [operation start]; 
    } 
} 

In risposta ottengo messaggio di errore indietro senza spiegazioni di errore:

{ 
    "error_code" = "upload_failed"; 
    "error_message" = "Upload failed"; 
    status = 0; 
} 

Qualcuno può aiutarmi? Qual è il modo corretto per farlo?

+0

hai provato a inviare 'NSData' invece di' NSString'? –

+0

sì, l'ho fatto e ricevendo la stessa risposta all'errore – Oleg

+0

hai provato con un'immagine molto piccola?/per garantire che non sia un problema di timeout –

risposta

3

Va bene, sono riuscito a risolvere il mio problema. Tutti i parametri devono essere impostati nel corpo del modulo, non come valori di richiesta. Sembra abbastanza semplice:

NSData *imageToUpload = UIImagePNGRepresentation([UIImage imageNamed:@"logo.png"]); 


    if (imageToUpload) 
    { 
     NSString *urlString = @"https://post.imageshack.us"; 

     AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; 
     NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData:imageToUpload name:@"fileupload" fileName:@"image" mimeType:@"image/png"]; 
      [formData appendPartWithFormData:[@"XXXXXX" dataUsingEncoding:NSUTF8StringEncoding] name:@"key"]; 
      [formData appendPartWithFormData:[@"json" dataUsingEncoding:NSUTF8StringEncoding] name:@"format"]; 
     }]; 



     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       NSLog(@"Upload Failed"); 
       return; 
      } 
      NSLog(@"error: %@", [operation error]); 

     }]; 

     [httpClient enqueueHTTPRequestOperation:operation]; 
    }} 

Spero che questo vi aiuterà qualcuno!

2

Prova questo e farci sapere se funziona:

 NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image,1.0);//(uploadedImgView.image); 
     if (imageToUpload) 
     { 
     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
     [parameters setObject:@"MY API KEY" forKey:@"key"]; 
     [parameters setObject:@"json" forKey:@"format"]; 

     AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]]; 

     NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"]; 
     }]; 

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      //NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       //NSLog(@"Upload Failed"); 
       return; 
      } 
      //NSLog(@"error: %@", [operation error]); 

     }]; 

     [operation start]; 
    } 
+0

Ancora la stessa risposta JSON con errore – Oleg

+0

ecco un link di una domanda che potrebbe aiutarti: http://stackoverflow.com/questions/ 931088/http-post-to-imageshack –

1

caricamento di un'immagine (di base)

Endpoint : https://post.imageshack.us/upload_api.php Parameters : 
* key : The api key for your application; found in email sent after filling out our API Key Request form 
* fileupload : image file 
* format : json tags : a CSV list of tags to describe your picture public : A string setting your image to public or private where "yes" is public and "no" is private. Images are public by default. 

Hai usato richiesta della chiave e ottenere la vostra chiave per il processo di caricamento?

Obtaining an API Key 
To obtain an API key, please use our API Key Request form. 

anche impostare formato application/json

+0

Sì, uso la chiave. Mi ha mostrato un errore che dovevo fare quando non l'ho usato, quindi sono sicuro che il problema non è legato alla chiave API – Oleg

+0

hai impostato il formato specificato in risposta e test? –

+0

Sì, l'ho fatto, senza fortuna :-( – Oleg

Problemi correlati