2011-06-09 42 views
8
請求時

我試圖請求這類URL在iPhone 4.0 SDK(訪問令牌略作修改,因爲你並不真的需要看到它):不良URL與NSURL

https://graph.facebook.com/me?sdk=ios&sdk_version=2&access_token=123902817987|8cb9e8408d2685cef853cd80.9-747882379|UGu5NvcAHiXuGEGzkq&format=json&limit=40&until=1286619821 

但我得到此消息:

Failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x9e657a0 {NSUnderlyingError=0x9e656a0 "bad URL", NSLocalizedDescription=bad URL} 

當我複製並粘貼Safari或Chrome時,它可以正常工作。我試圖用建議的here替換|%|,但不起作用。

要求它從終端看起來是這樣的:

curl https://graph.facebook.com/me/statuses?sdk=ios&sdk_version=2&access_token=9999955817987|8ab9e8408d2685cef.3-747882379|UGuxWDuM&format=json&limit=40&until=1286619821 
[1] 16190 
[2] 16191 
[3] 16194 
[4] 16195 
[5] 16196 
[2] Done     sdk_version=2 
[4]- Done     format=json 
$ -bash: UGu5NvcAHiXuGEGzkq3KP8xWDuM: command not found 
-bash: 8ab9e8408d2685cef853cd80.3-747882379: command not found 
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}} 
[1] Done     curl https://graph.facebook.com/me/statuses?sdk=ios 
[3]- Exit 127    access_token=9999955817987 | 8ab9e8408d2685cef.3-747882379 | UGuxWDuM 
[5]+ Done     limit=40 

任何想法?

+0

啊......我剛剛找到答案:http://stackoverflow.com/questions/6162653/ios-develoment-why-is-my-nsurlconnection-failing-with-a-bad-url-error-for只有 – 2011-06-09 01:58:31

回答

21
NSString *strURL = [loc_address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

      // requesting weather for this location ... 
      NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://www.google.com/ig/api?weather=%@", strURL]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; 
      [req setHTTPMethod:@"POST"]; 
      [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
+1

非常感謝,謝謝 – SampathKumar 2012-10-26 12:11:15

+0

嗨敏奴,我也遇到了不好的網址,並找出了編碼是必須的 – Rakesh 2012-10-27 09:00:22

+1

也謝謝我。 – 2014-07-02 12:53:07

7

您需要將URL中的管道字符替換爲%7C。這是網址編碼值。

此外,URL需要用引號括起來,以便它可以使用curl。

0

ho ho ho。

設置location_id使用您想要的場地的ID。

這讓我浪費了很多時間。

十幾個Facebook的祕密之一是調試網絡時,你想要什麼,在這種情況下創建一個事件。你會看到你需要的正確的參數名稱。

0

此錯誤消息也可能由URL打字中的任何錯誤引起。通常在URL字符串中有一些空格。