手机获取服务器延迟,在iPhone上从Web服务器请求数据会造成太多延迟

SBJSON *json = [SBJSON new];

json.humanReadable = YES;

NSString *service = @"/getUserInfo";

//NSString *requestString = [NSString stringWithFormat:@"{"method":"%@"}", service];

NSString *requestString = [NSString stringWithFormat:@"{"firstname":"%@"}",firstName.text,nil];

NSLog(@"Request String: %@", requestString);

NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"plist" ];

NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];

NSString *urlLoc = [fileContents objectForKey:@"baseURL"];

urlLoc = [urlLoc stringByAppendingString:service];

NSLog(@"URL is %@",urlLoc);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:

[NSURL URLWithString: urlLoc]];

NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];

[request setHTTPMethod: @"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody: requestData];

//Data returned by WebService

NSError *respError = nil;

NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];

if (respError) {

NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@",

[respError localizedDescription],

[[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Check your network connection"

message:msg delegate:self cancelButtonTitle:@"OK"

otherButtonTitles:nil];

[alertView show];

NSArray *keys = [NSArray arrayWithObjects:@"firstname", @"lastname", nil];

NSArray *objects = [NSArray arrayWithObjects:@"failed to", @"refresh data...", nil];

NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

allUsers = [[NSArray alloc] initWithObjects:dictionary, nil];

//[self setUserData:allUsers];

//[tblView reloadData];

//[allUsers release];

}

else

{

NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

NSDictionary *results = [responseString JSONValue];

// Additional steps as the webservice is adding an additional "{d:" so stripping of that

NSString *extractUsers = [results objectForKey:@"d"];

// The actual string that Web services returned, so re-scan the same and convert it as object

NSDictionary *finalResult = [extractUsers JSONValue];

allUsers = [finalResult objectForKey:@"users"];

NSLog(@"Data is : %@",allUsers);

NSLog(@"Final Value is : %@",[[allUsers objectAtIndex:0] valueForKey:@"lastname"]);

if([allUsers count]>0)

{

label.text = [[allUsers objectAtIndex:0] valueForKey:@"lastname"];

}

else

{

label.text = @"";

}

[responseString release];

[request release];

}

[inProgressIndicator stopAnimating];

[fileContents release];

//Release all the allocated data

[json release];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值