NSURLCподключение заморозки - PullRequest
0 голосов
/ 14 марта 2012

Я пытаюсь отправить сообщение JSON с помощью NSURLConnection:

NSURL *url = [NSURL URLWithString:urlStr];

NSString *jsonPostBody = [NSString stringWithFormat:@"{\"user\":""\"%@\""",\"pass\":""\"%@\""",\"listades\":\"\"}",
                          [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                          [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSLog(@"JSNON BODY:%@", jsonPostBody);
NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[request setTimeoutInterval:2.0];
NSString* postDataLengthString = [[NSString alloc] initWithFormat:@"%d", [postData length]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:postDataLengthString forHTTPHeaderField:@"Content-Length"];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

Выполнение приложения останавливается при достижении команды подключения, несмотря на тайм-аут.

Я застрял в этой проблеме и не могу найти решение.

Большое спасибо

Ответы [ 2 ]

2 голосов
/ 14 марта 2012
-(void) jsonRESTWebServiceMethod:(NSString*)method WithParameter:(NSMutableDictionary*)params{
    self.iResponseType = JSONTYPE;
    NSMutableDictionary *bodyDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:method,@"name",params,@"body",nil,nil];
    NSData *data = [[CJSONSerializer serializer] serializeObject:bodyDict error:nil];
    NSString *strRequest = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    strRequest = [NSString stringWithFormat:@"json=%@",strRequest];
    NSData *body = [strRequest dataUsingEncoding:NSUTF8StringEncoding];
    NSString *strURL = [NSString stringWithString:WebServiceURL];
    NSString* escapedUrlString = [strURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

    NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:escapedUrlString]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:body];
    NSString *msgLength = [NSString stringWithFormat:@"%d",strRequest.length];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];

    if (mydata) {
        [mydata release];
        mydata = nil;
    }
    conections = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    mydata = [[NSMutableData alloc] init];
}
1 голос
/ 14 марта 2012

Это замораживает ваше приложение, потому что вы сразу передали YES в start. Это приведет к зависанию приложения до завершения запроса.

Вам нужно использовать что-то вроде connectionWithRequest: Delegate: - это запустит запрос в фоновом режиме и сообщит вам, когда это будет сделано.

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...