2

Objective C コードから特定の URL への Http GET 要求をセットアップしようとしています。何をしても、どの URL を使用しても、次のエラーが表示されます。

Connection Failed: Error Domain=NSURLErrorDomain 
Code=-1000 "bad URL" UserInfo=0x755c440 
{NSUnderlyingError=0x7561950 "bad URL", NSLocalizedDescription=bad URL}

私は iOS 開発に不慣れなので、これを正しく行っているかどうかはわかりません。Apple のドキュメントに従い、さまざまなフォーラムで回答を探しましたが、それでもこのエラーが発生します。

これが私のコードです:

NSString *url = [NSString stringWithFormat: @"http://example.com/api/%@/%@", str1, str2];

NSString *encodedUrl = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

NSLog(@"URL - %@", encodedUrl);              // Checking the url

NSMutableURLRequest *theRequest= [[NSMutableURLRequest alloc] init];

[theRequest setHTTPMethod:@"GET"];


[NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:10.0];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

どんな提案でも大歓迎です。

4

1 に答える 1

4

を初期化していませんtheRequest。戻り値を変数に割り当てていない場合、この行は何もしません。

[NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:10.0];

これは、使用する必要があるコードです。

NSString *url = [NSString stringWithFormat: @"http://example.com/api/%@/%@", str1, str2];

NSString *encodedUrl = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

NSLog(@"URL - %@", encodedUrl);              // Checking the url

NSMutableURLRequest *theRequest= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:10.0];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
于 2012-12-17T12:55:26.240 に答える