连接失败JSON流解析在IOS
我想使用简单的登录使用JSON和Web服务。使用网关用户名和密码阻止Web服务。需要哪些代码才能将用户名和密码传递给网关?这是为什么发生?我得到一个响应代码为0。此外,如果有人可以提供一个很好的教程,了解有关Web服务和JSON在iOS中。提前致谢。连接失败JSON流解析在IOS
这是我的代码。代码写入UIButton
NSString *post = [NSString stringWithFormat:@"&Email=%@&Password=%@",
[self.emailTextField text], [self.passwordTextfield text]];
NSLog(@"PostData: %@",post);
NSURL *url = [NSURL URLWithString:@"http:/Gateway/api/json/reply/ZympayAuthenticateCustomer"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300) {
NSString *responseData = [[NSString alloc]initWithData:urlData
encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
NSError *error = nil;
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingMutableContainers
error:&error];
NSLog(@"Data: %@",jsonData);
NSLog(@"Success: %ld",(long)success);
if (success == 1) {
NSLog(@"Login SUCCESS");
}
else {
NSString *error_msg = (NSString *) jsonData[@"error_message"];
[self alertStatus:error_msg :@"Sign in Failed!" :0];
}
}
else {
//if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
}
}
}
@catch (NSException * e)
{
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
if (success)
{
NSLog(@"THIS IS SUCCESS");
// [self performSegueWithIdentifier:@"login_success" sender:self];
}
}
尝试此
NSURL * URL = [NSURL URLWithString:@ “http://www.site.com/test_json_link/?username= 'USERNAME' &密码= '密码'”];
现在,防止我的Web服务器重定向到登录页面。我创建了一个中间件来处理上面的url。我定制的中间件有这样的吧:
if request.path != '/accounts/login/' and request.user.is_anonymous(): url = request.path.split('/')[1] if url == 'test_json': username = request.GET['username'] password = request.GET['password'] user = authenticate(username = username, password = password) if user is not None: if user.is_active: login(request, user)
现在我应该在哪里添加中间件代码? – iOSDeveloper
代码中有许多问题。
单一个使你的做法很可能会失败,是你使用 NSURLConnection
方便类方法sendSynchronousRequest:
:如果请求需要认证,证书必须在URL中传递。
参见:sendSynchronousRequest:returningResponse:error:
此外,你有没有办法取消请求,并在请求重定向采取适当的行动。
因此,您的方法的第一个重大变化是使用NSURLConnection
的代表方法,或可能使用NSURLSession
和NSURLSessionTask
。 SO上有相当多的解决方案,可以让你快速实施自己的解决方案。
但是,推荐的解决方案是使用第三方库。
为什么你发送的内容为'应用程序/ x-WWW的形式,urlencoded'?这通常很容易出错,并且您没有正确传递参数(忽略适当的百分比编码)。无论如何,使用** JSON **作为你的内容类型。 – CouchDeveloper