簡體   English   中英

向Twitter發送消息會創建錯誤消息

[英]Sending message to Twitter creates an error message

我正在嘗試運行以下代碼(來自Heads First iPhone Development一書-第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返回的響應是:

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

有任何想法嗎?

看來您應該在格式字符串中使用“%@”而不是“%”:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM