2009-11-21 116 views
0

我試圖讓下面的代碼運行(它來自Heads First iPhone Development的書 - 第81頁),它是一個推特應用程序,當你按下按鈕發送一個簡單的短信,Twitter的:發送消息給Twitter創建一條錯誤消息

//TWITTER BLACK MAGIC 
    NSMutableURLRequest *theRequest=[NSMutableURLRequest 
requestWithURL:[NSURL URLWithString:@"http://username:[email protected]/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上的結果 - 在resopnse我回來從Twitter在調試窗口是:

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

任何想法?

回答

2

看起來你應該使用「%@」,而不是在你的格式字符串「%」:

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

嗯,它真的很容易誤讀的OBJ-C,當你剛剛起步。謝謝,它現在一切正常! – Vidar 2009-11-21 22:44:54