NSURLConnection冻结

时间:2012-03-14 01:51:39

标签: objective-c ios json ios5

我正在尝试使用NSURLConnection发送JSON帖子:

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 个答案:

答案 0 :(得分:2)

-(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 :(得分:1)

它冻结了你的应用,因为你已经将YES传递给了startImmediately。这将冻结应用程序直到请求完成。

你需要使用像connectionWithRequest:delegate这样的东西: - 这将在后台运行请求并告诉你它什么时候完成。

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];