ASIHTTPRequest POST方法执行两次

时间:2014-01-09 19:18:33

标签: ios asihttprequest

我正在尝试使用ASIHTTPRequest从服务器下载一些数据,但是没有理由这个代码所在的方法被执行两次,这里是代码

- (void)viewDidLoad
{
        [super viewDidLoad];
        [viewDisabled setHidden:FALSE];
        [activityBusy startAnimating];
        [self downloadData];

        [viewDisabled setHidden:TRUE];
        [activityBusy stopAnimating];
}
-(void) downloadData
{
    // download ingredients
    NSString *str = [NSString stringWithFormat:@"http://im.com/wrapper/wrapperSL.svc/downloaddata"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];

    // create JSON input body
    NSMutableString *json = [[NSMutableString alloc] initWithCapacity:0];
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSInteger langId = [defs integerForKey:@"language"];

    [json appendFormat:@"{\"LanguageId\":\"%d\"}",langId];

    NSLog(json);

    // init request and call
    [request setRequestMethod:@"POST"];
    // [request setDelegate:self];
    [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
    [request addRequestHeader:@"Accept" value:@"application/json"];
    [request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];

    request.shouldAttemptPersistentConnection = NO;

    [request startSynchronous];


    NSString *response;

    NSError *error = [request error];
    if (!error) {
        response = [request responseString];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Downloading App data failed!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
}

以某种方式,请求被完整地发送两次方法本身被执行两次并且我接收数据两次。

请建议解决方案

0 个答案:

没有答案