English 中文(简体)
twitpic API not connecting (multipart/form-data) objective-c
原标题:

I ve put together this code from lots of research. Information on the twitpic API is here:

http://twitpic.com/api.do#uploadAndPost

Through debugging i can tell that the dictionary is good, and that it gets through all the methods, the request gets submitted, but then the NSLog method returns <>.

I m not sure where I could be going wrong, i don t know much about the multipart/form-data structure. Maybe I m doing something wrong with my connection?

Code below.

 -(void)uploadBoth {  
      //Create dictionary of post arguments
      NSArray *keys = [[NSArray alloc] initWithObjects:@"media",@"username",@"password",@"message",nil];
      NSArray *objects = [[NSArray alloc] initWithObjects:
           imageData, twitterUserSave, twitterPassSave, [NSString stringWithFormat:@"%@",messageView.text], nil];

      NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
     NSLog(@"%@", keysDict);

      //create twitpic photo post
      NSURLRequest *twitpicPost = [self twitpicRequest:keysDict withData:imageData];

      //send request, return YES if successful
      NSURLConnection *twitpicConnection = [[NSURLConnection alloc] initWithRequest: twitpicPost delegate:self];

      if (!twitpicConnection) {
       NSLog(@"Failed to submit request");
      } else {
       NSLog(@"Request submitted");
       NSData *receivedData = [[NSMutableData data] retain];
       NSLog(@"%@", receivedData); // THIS PART RETURNS <>
      }
    }

The NSURLRequest part

-(NSURLRequest *)twitpicRequest:(NSDictionary *)postKeys withData:(NSData *)data {

 NSLog(@"got this far");

  //create the URL POST Request to twitpic
  NSURL *twitpicURL = [NSURL URLWithString:@"http://twitpic.com/api/uploadAndPost"];
  NSMutableURLRequest *twitpicPost = [NSMutableURLRequest requestWithURL:twitpicURL];
  [twitpicPost setHTTPMethod:@"POST"];

  //Add the header info
  NSString *stringBoundary = [NSString stringWithString:@"0xAbCdEfGbOuNdArY"];
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
  [twitpicPost addValue:contentType forHTTPHeaderField: @"Content-Type"];

  //create the body
  NSMutableData *postBody = [NSMutableData data];
  [postBody appendData:[[NSString stringWithFormat:@"--%@
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

  //add key values from the NSDictionary object
  NSEnumerator *keys = [postKeys keyEnumerator];
  int i;
  for (i = 0; i < [postKeys count]; i++) {
   NSString *tempKey = [keys nextObject];
   [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"

",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"
--%@
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  }

  //add data field and file data
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="data"
"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[NSData dataWithData:data]];
  [postBody appendData:[[NSString stringWithFormat:@"
--%@--
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

  //add the body to the post
  [twitpicPost setHTTPBody:postBody];

 return twitpicPost;
}
问题回答

Your output makes sense given that you print the data without waiting for it to download:

NSData *receivedData = [[NSMutableData data] retain]; // this is a new empty data object
NSLog(@"%@", receivedData); // THIS PART RETURNS <>

You should declare "receivedData" as an "NSMutableData" reference at the top of the implementation file (or as an instance variable in the class header). Then, you should implement the delegate methods as described in listings 2-5 of the Using NSURLConnection.

Otherwise, you may want to start by trying to get a synchronous request to work:

NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:twitpicPost returningResponse:&response error:&error];
NSLog(@"%@",[[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签