現在のプロジェクトでは、パラメーター値を URL に送信し、送信されたパラメーター値に基づいてファイルをxcode
取得する必要があります。xml
以下のコードを試しましたが、うまくいきません。
(IBAction)myButtonClick:(id)sender
{
NSURL *oRequestURL =
[NSURL URLWithString:@"http://xyz .com/air/1.0/search?from=MAA&to=CJB&depart-date=2012-06-30&adults=2&children=2&infants=1&way=one&cabin-type=Economy&sort=asc"];
NSMutableURLRequest *oRequest = [[[NSMutableURLRequest alloc]init]autorelease];
[oRequest setHTTPMethod:@"POST"];
[oRequest setURL: oRequestURL];
NSMutableData *oHttpBody = [NSMutableData data];
[oHttpBody appendData:[@"This is HTTP Request body" dataUsingEncoding:NSUTF8StringEncoding]];
[oRequest setValue:[oHttpBody length] forHTTPHeaderField:@"Content-Length"];
NSError *oError = [[NSError alloc]init];
NSHTTPURLResponse *oResponseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:oRequest returningResponse:oResponseCode error:oError];
if ([oResponseCode statusCode]> 200) {
NSLog(@"Status code is greater than 200");
}
NSString *strResult=[[NSString alloc]initWithData:oResponseData encoding:NSUTF8StringEncoding];
NSLog(@"The result is %s",strResult);
}
多くのサイトや書籍を検索しましたが、解決策が見つかりませんでした。チュートリアルやその他の有用なリソースへのリンクを提供できれば、非常に役立ちます。大変お世話になりました。
ありがとうございました。
こんにちは、私は解決策を見つけました。コードは以下の通りです。それが他の誰かに役立つことを願っています:)
- (IBAction)myButtonPressed:(id)sender
{
NSString *urlAsString = @"http://api.abc.com/air/1.0/search?from=MAA&to=CJB&depart-date=2012-09-30&adults=2&children=2&infants=1&way=one&cabin-type=Economy&sort=asc";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setValue:@"2193141de2g7e2a34bb19bc3aa52b3b5" forHTTPHeaderField:@"X-XX-API-KEY"];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection
sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length]>0 &&
error == nil)
{
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html);
}
else if ([data length]== 0 && error==nil) {
NSLog(@"Nothing was downloaded");
}
else if (error!= nil) {
NSLog(@"Error occured = %@", error);
}
}];
}