こんにちは、私はiPhoneの開発に慣れていないので、データをURLに投稿して成功した応答を取得する方法を説明してください。以下のコード例です...
-(IBAction)clicked:(id)sender{
NSString *CIDString = cID.text;
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/test/?"];
NSString *postData = [NSString stringWithFormat:@"companyID=%@",CIDString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
[self startConnection:(NSMutableURLRequest *)request];
if([self.result isEqualToString:@"New Alert"])
{
cID.text = @"Scuess";
}
}
そしてstartConnectionは次のとおりです
- (void)startConnection:(NSMutableURLRequest *)request {
[self.connection cancel];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData = data;
self.result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"receivedData: %@", [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding]);
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (self.networkErrorAlert) {
NSLog(@"connection fail");
}
[self.connection start];
}
誰か助けてくれませんか...