0

我从大量研究中汇总了这段代码。关于 twitpic API 的信息在这里:

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

通过调试,我可以看出字典是好的,它通过了所有的方法,请求被提交,但是 NSLog 方法返回 <>。

我不确定哪里可能出错,我对多部分/表单数据结构了解不多。也许我的连接有问题?

代码如下。

 -(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 <>
      }
    }

NSURLRequest 部分

-(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:@"--%@\r\n",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=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
   [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
  }

  //add data field and file data
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[NSData dataWithData:data]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

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

 return twitpicPost;
}
4

1 回答 1

1

您的输出是有意义的,因为您无需等待下载即可打印数据:

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

您应该在实现文件的顶部将“receivedData”声明为“NSMutableData”引用(或作为类头中的实例变量)。然后,您应该实现使用 NSURLConnection清单 2-5 中描述的委托方法。

否则,您可能希望首先尝试使同步请求起作用:

NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:twitpicPost returningResponse:&response error:&error];
NSLog(@"%@",[[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]);
于 2009-11-22T03:57:27.147 回答