-1

我的 java webservice 中有一个方法,它接受参数并运行 sql 查询来更新数据库。参数当然是从 iphone 插入到 URL 中,当 url 执行时它会调用 webservice 方法来更新数据库。我的问题是,如何在 ios 中执行 URL 但不打开 safari 或其他任何东西。这里是代码:

-(void)updateInfo
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Updating Profile" message:@"Your data have been saved." delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert setTag:02];
    [alert show];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    GlobalObjects *global = [[GlobalObjects alloc]init];
    NSString *url =[NSString stringWithFormat:@"%@/profile/login/update/%@/%@/%@/%@/%@/%@/%@",global.domain,self.user, self.firstName.text,self.address.text,self.city.text,self.country.text,self.zipCode.text,self.phone.text];

    if (alertView.tag == 02 && buttonIndex != alertView.cancelButtonIndex) {
        // The URL execution method should come here.

    }
}
4

1 回答 1

1

一个绝对最小值(不是好的做法)的 NSURLConnection 示例:

NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
                                      returningResponse:&response
                                                  error:&error];

if (error == nil)
{
    // Parse data here
}

您应该真正使用异步版本,以免阻塞主线程。检查这个

于 2013-08-23T22:05:12.593 回答