我有一个iOS应用程序,用户必须在使用该应用程序之前注册。我在Storyboard中创建了UI,我正在从UITextfields中读取用户详细信息。然后,我将详细信息发送到Register API,发送回JSON响应。我正在使用NSURLConnection进行通信。
以下是我从测试网址收到的回复 - 仅用于测试目的: 的 {"用户名":"汉斯""密码":"汉斯"}
然而,当我尝试读取密码以确保用户不存在时(再次,仅用于测试目的),我返回nil以返回密码值。
在我的.h文件中,我声明了数据和连接:
@interface RegisterViewController : UIViewController <NSURLConnectionDataDelegate>
{
// Conform to the NSURLConnectionDelegate protocol and declare an instance variable to hold the response data
NSMutableData *buffer;
NSURLConnection *myNSURLConnection;
}
在我的.m文件中,当有人点击REGISTER按钮时,我创建请求并启动连接,如下所示。我在示例中给出了一个虚拟URL,但我收到的响应是: 的 {&#34;用户名&#34;:&#34;汉斯&#34;&#34;密码&#34;:&#34;汉斯&#34;}
- (IBAction)registerButtonClicked:(id)sender
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://myDummyURL/Login.php"]];
// Construct the JSON Data
NSDictionary *stringDataDictionary = @{@"firstname": firstname, @"lastname": lastname, @"email": email, @"password" : password, @"telephone" : telephone};
NSError *error;
NSData *requestBodyData = [NSJSONSerialization dataWithJSONObject:stringDataDictionary options:0 error:&error];
// Specify that it will be a POST request
[request setHTTPMethod:@"POST"];
// Set header fields
[request setValue:@"text/plain" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestBodyData];
myNSURLConnection = [NSURLConnection connectionWithRequest:request delegate:self];
// Ensure the connection was created
if (myNSURLConnection)
{
// Initialize the buffer
buffer = [NSMutableData data];
// Start the request
[myNSURLConnection start];
}
}
此处创建连接没有问题。
在我的.m文件中,我实现了委托方法,在connectionDidFinishLoading()中,我尝试读取返回的JSON。以下是我正在使用的代码。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Dispatch off the main queue for JSON processing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSString *jsonString = [[NSJSONSerialization JSONObjectWithData:buffer options:0 error:&error] description];
// Dispatch back to the main queue for UI
dispatch_async(dispatch_get_main_queue(), ^{
// Check for a JSON error
if (!error)
{
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSDictionary *dictionary = [jsonArray objectAtIndex:0];
NSString *test = [dictionary objectForKey:@"password"];
NSLog(@"Test is: %@", test);
}
else
{
NSLog(@"JSON Error: %@", [error localizedDescription]);
}
// Stop animating the Progress HUD
});
});
}
从下面的日志屏幕抓取,您可以看到返回的jsonString具有值,但jsonArray始终为nil。错误读取:错误NSError * domain:@&#34; NSCocoaErrorDomain&#34; - 代码:3840 0x00007ff158498be0
提前致谢。
答案 0 :(得分:2)
您的jsonString
实际上是由NSDictionary
创建的NSJSONSerialization
对象 - 您正在寻找,没有数组。在JSON字符串中,花括号{}
表示字典,方括号[]
表示数组
试试这个
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Dispatch off the main queue for JSON processing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:buffer options:0 error:&error];
// Dispatch back to the main queue for UI
dispatch_async(dispatch_get_main_queue(), ^{
// Check for a JSON error
if (!error)
{
NSString *test = [dictionary objectForKey:@"password"];
NSLog(@"Test is: %@", test);
}
else
{
NSLog(@"JSON Error: %@", [error localizedDescription]);
}
// Stop animating the Progress HUD
});
});
}
修改:我忽略了description
行末尾的NSJSONSerialization
方法。当然必须删除。
答案 1 :(得分:1)
您的代码有两个问题:
这必须解决您的问题:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Dispatch off the main queue for JSON processing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:buffer options:0 error:&error];
// Dispatch back to the main queue for UI
dispatch_async(dispatch_get_main_queue(), ^{
// Check for a JSON error
if (!error)
{
NSString *test = [dictionary objectForKey:@"password"];
NSLog(@"Test is: %@", test);
}
else
{
NSLog(@"JSON Error: %@", [error localizedDescription]);
}
// Stop animating the Progress HUD
});
});
}