对于这个问题我很抱歉。我是iOS的新手。
我正在编写xcode 7.1。我在我的Local server上调用一个Local server,但是我得到了这个错误。我不知道为什么。我已经尝试和寻找了几天,但我找不到任何相关的东西。这是我的密码
NSString *myUrlString = [NSString stringWithFormat:@"%@%@/login",link,Entry ];
//create string for parameters that we need to send in the HTTP POST body
NSLog(@"My Url = %@",myUrlString);
NSMutableDictionary* postRequestDictionary = [[NSMutableDictionary alloc] init];
postRequestDictionary[@"email" ]= EmailIDTF.text;
postRequestDictionary[@"password" ]= PasswordTF.text;
NSLog(@"body = %@",postRequestDictionary);
NSData *json;
NSString *jsonString;
NSError *error;
// Dictionary convertable to JSON ?
if ([NSJSONSerialization isValidJSONObject:postRequestDictionary])
{
// Serialize the dictionary
json = [NSJSONSerialization dataWithJSONObject:postRequestDictionary options:NSJSONWritingPrettyPrinted error:&error];
// If no errors, let's view the JSON
if (json != nil && error == nil)
{
jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];
NSLog(@"JSON: %@", jsonString);
}
}
//create a mutable HTTP request
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[myUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0]; //sets the receiver’s timeout interval, in seconds
[urlRequest setTimeoutInterval:30.0f];
//sets the receiver’s HTTP request method
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-type"];
NSString *params = [NSString stringWithFormat:@"%@",jsonString];
NSLog(@"param = %@",params);
//sets the request body of the receiver to the specified data.
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
//allocate a new operation queue
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//Loads the data for a URL request and executes a handler block on an
//operation queue when the request completes or fails.
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error == nil){
//process the JSON response
//use the main queue so that we can interact with the screen
dispatch_async(dispatch_get_main_queue(), ^{
[self parseResponse1:data];
});
}
else if ([data length] == 0 && error == nil){
NSLog(@"Empty Response, not sure why?");
}
else if (error != nil){
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Not again, what is the error = %@", error);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert!" message:@"Please check that you are connected to internet." delegate:self cancelButtonTitle:@"I got it." otherButtonTitles: nil];
// spinnerview.hidden=YES;
[alert show];
});
}
}];但我所得到的只是这个错误
错误UserInfo={NSErrorFailingURLKey=http://xyz/login,Domain=NSURLErrorDomain代码=-1012“(空)”UserInfo={_kCFURLErrorAuthFailedResponseKey={url NSErrorFailingURLStringKey=http://xyz/login,NSUnderlyingError=0x165a0f80 {Error Domain=kCFErrorDomainCFNetwork Code=-1012 "(null)“UserInfo={_kCFURLErrorAuthFailedResponseKey={url=http://xyz/login}
问题是它为什么要给“(空)”?请帮帮我伙计们。提前谢谢。
发布于 2015-12-04 08:27:26
在您的info.plist文件中添加此键后,尝试:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>希望这能有所帮助。
发布于 2015-12-04 09:14:04
使用以下代码调用web服务
NSString *myUrlString = [NSString stringWithFormat:@"%@%@/login",link,Entry ];
NSMutableDictionary* postRequestDictionary = [[NSMutableDictionary alloc] init];
postRequestDictionary[@"email" ]= EmailIDTF.text;
postRequestDictionary[@"password" ]= PasswordTF.text;
NSString *mystring=[self returnMeParameterString:postRequestDictionary];
NSURL *url = [NSURL URLWithString:myUrlString];
NSData *postData = [mystring dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[mystring length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse response, NSData data, NSError *connectionError) {
if(connectionError)
{
//error
}
else
{
//success
}
}];pragma标记将字典转换为字符串
-(NSString*)returnMeParameterString:(NSDictionary *)params{
NSMutableString *paramstring = [[NSMutableString alloc] init];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
NSMutableArray *valueArray = [[NSMutableArray alloc] init];
for( NSString *aKey in [params allKeys] )
{
[keyArray addObject:aKey];
}
for( NSString *aValue in [params allValues] )
{
[valueArray addObject:aValue];
}
for (int k=0; k< keyArray.count; k++)
{
NSString *tempString;
if(k==0)
{
tempString = [NSString stringWithFormat:@"%@=%@",[keyArray objectAtIndex:k],[valueArray objectAtIndex:k]];
}
else
{
tempString = [NSString stringWithFormat:@"&%@=%@",[keyArray objectAtIndex:k],[valueArray objectAtIndex:k]];
}
[paramstring appendString:tempString];
}
return paramstring;
}https://stackoverflow.com/questions/34082743
复制相似问题