서버에서 데이터를 가져 오는 데 사용되는 클래스가 있습니다. 내 서버에서 반환 된 데이터는 JSON에 있습니다. 어떤 이유 때문에 didReceiveData가 전혀 실행되지 않습니다. 테스트를 위해 NSLog를 배치했지만 아무 것도하지 않습니까?didReceiveData 함수가 작동하지 않는 이유
+(NSJSONSerialization *) getTask:(id)task_id{
NSString *post = [NSString stringWithFormat:@"&task_id=%@", task_id];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://my-server.com/"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn){
NSLog(@"Testing");
}
return json;
}
// Log the response for debugging
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
NSLog(@"test");
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
}
// Declare any connection errors
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error: %@", error);
}
감사합니다,
피터 당신은 연결을 시작하는 데 필요한
NSURLConnection이 * 비동기 적으로 작동하기 때문에 * getTask : 메서드에서받은 JSON을 * 리턴 할 수 없다는 것에주의하십시오. - 아마도'getTask :'에서 리턴 한 후 연결을 취소 할 것인가? –
잘 모르겠습니다. 그렇다면 NSURLConnection을 클래스에 어떻게 배치 할 수 있습니까? –
위의 코드는 내 앱 전체에 걸쳐 여러 번 사용되기 때문에 클래스에 배치 한 이유는 무엇입니까? –