向Twitter发送消息会创建错误消息

时间:2009-11-21 22:23:59

标签: iphone objective-c twitter

我正在尝试运行以下代码(它来自Heads First iPhone开发书 - 第81页),它是一个Twitter应用程序,当您按下按钮发送简单的短信时,代码会运行Twitter的:

//TWITTER BLACK MAGIC
    NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:[NSURL URLWithString:@"http://username:password@twitter.com/statuses/update.xml"]
cachePolicy:NSURLRequestUseProtocolCachePolicy 

    timeoutInterval: 60.0];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[[NSString stringWithFormat:@"status=%", themessage] dataUsingEncoding:NSASCIIStringEncoding]];

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

并且它确实在一定程度上,即代码运行但你无法在Twitter上看到结果 - 我在调试窗口中从Twitter返回的resopnse是:

  <request>/statuses/update.xml</request>
  <error>Client must provide a 'status' parameter with a value.</error>

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

看起来您应该在格式字符串中使用'%@'而不是'%':

[theRequest setHTTPBody:[[NSString stringWithFormat:@"status=%@", themessage] dataUsingEncoding:NSASCIIStringEncoding]];