我通过单击按钮调用服务器,连接didReceiveData
返回 aNull
并使应用程序崩溃。但是有一个条件来检查数据是否是if
,在条件下再次启动服务器调用是执行此操作的最佳实践吗?else
Null
if
-(IBAction)btnClicked:(id)sender
{
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:url];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[storeRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[storeRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[storeRequest setHTTPBody:postData];
NSURLConnection *theConnection;
theConnection =[[NSURLConnection alloc] initWithRequest:storeRequest delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
if (data1==nil)
{
[self downloadDetails];//Restart the download
}
else
{
receivedData =[[NSMutableData alloc]init];
[receivedData appendData:data1];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError* error;
NSMutableArray* jsonArray = [NSJSONSerialization
JSONObjectWithData:receivedData
options:kNilOptions
error:&error];
[appDelegate.inboxArray addObjectsFromArray:jsonArray];
}