在.h文件中声明
NSString *extractUsersGRC;
.m文件
{
..
extractUsersGRC=[[NSString alloc]init];
extractUsersGRC = [[resultsGRC objectForKey:@"d"] retain];
NSDictionary *dict1 =[[NSDictionary alloc]init];
dict1=[[extractUsersGRC JSONValue]retain];
}
我正在使用json从Web服务和Web服务获取数据 重播我的请求,但有时我得 dict1 为零。 jsonvalue返回null。所以我犯了错误。
extractUsersGRC保存数据,但Jsonvalue返回null ..?为什么?我是 没有得到帮助我。
SBJSON *jsonGRC = [SBJSON new];
jsonGRC.humanReadable = YES;
responseData = [[NSMutableData data] retain];
NSString *service = @"/GET_Recent_Activity";
NSString *flagval=@"C";
double latval=[[[NSUserDefaults standardUserDefaults]valueForKey:@"LATITUDE"]doubleValue];
double longval=[[[NSUserDefaults standardUserDefaults]valueForKey:@"LONGITUDE"]doubleValue];
NSString *userid=[[NSUserDefaults standardUserDefaults]valueForKey:@"UserID"];
long u_id= [userid longLongValue];
NSLog(@"%ld",u_id);
NSString *requestString = [NSString stringWithFormat:@"{\"flag\":\"%@\",\"current_Lat\":\"%f\",\"current_Long\":\"%f\",\"userid\":\"%ld\"}",flagval,latval,longval,u_id];
NSLog(@"request string:%@",requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
fileContentsGRC = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
urlLocGRC = [fileContentsGRC objectForKey:@"URL"];
urlLocGRC = [urlLocGRC stringByAppendingString:service];
NSLog(@"URL : %@",urlLocGRC);
requestGRC = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLocGRC]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[requestGRC setHTTPMethod: @"POST"];
[requestGRC setValue:postLength forHTTPHeaderField:@"Content-Length"];
[requestGRC setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestGRC setHTTPBody: requestData];
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: requestGRC returningResponse: nil error: &respError ];
答案 0 :(得分:0)
在@property (nonatomic, strong) NSMutableData *returnData;
档案中声明.h
跟我来
更改您的NSURLConnection
声明
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
self.returnData = [[NSMutableData alloc] init];
else
NSLog(@"Connection Failed!");
并委托NSURLConnection
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.returnData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.returnData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Connection failed." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonString = [[NSString alloc] initWithData:self.returnData encoding:NSUTF8StringEncoding];
NSMutableDictionary *jsonDictionary = [jsonString JSONValue];
NSLog(@"%@", jsonDictionary);
}