[英]How to resolve error:the operation couldn’t be completed. (cocoa error 3840.) json iOS?
I wrote the GET method code to retrieve the details from server(json format).But I am getting the error (the operation could not be completed). 我写了GET方法代码来从服务器(json格式)中检索详细信息,但是我遇到了错误(操作无法完成)。 can anyone please provide me some information regarding this?
有人可以向我提供有关此信息吗? My code:
我的代码:
-(void)getResponseWithOutBodywithMethod:(NSString*)method
{
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:method];
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// [request setTimeoutInterval:20.0];
NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData alloc] init] ;
}
}
#pragma mark NSURLConnection delegate methods
- (void) connection:(NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response{
/* This method is called when the server has determined that it has
enough information to create the NSURLResponse. It can be called
multiple times, for example in the case of a redirect, so each time
we reset the data. */
[receivedData setLength:0];
}
- (void) connection:(NSURLConnection *)connection didReceiveData: ( NSData *)data{
[receivedData appendData:data];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:( NSError *)error{
completion(nil, error);
NSLog(@"%@",[error localizedDescription]);
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection{
if (receivedData != nil) {
NSError *jsonParsingError = nil;
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:receivedData
options:kNilOptions error:&jsonParsingError];
NSString* str = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"error %@",jsonParsingError.localizedDescription);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ER" message:str delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
if (dict == nil && [dict count]==0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:jsonParsingError.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
completion(nil, jsonParsingError);
}
else {
completion(dict, nil);
}
}
else {
completion(nil, [NSError errorWithDomain:@"http" code:-1 userInfo:nil]); //todo proper error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get data." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}
Well I would think your Json is the problem. 好吧,我认为您的Json是问题所在。
If your server is responding: 如果您的服务器响应:
Something=somethingelse 某物=其他
That's not the proper format. 那不是正确的格式。 You would need to Json encode it so it looks like:
您将需要对Json进行编码,使其看起来像:
[{"key":"value", "key":"value"}] [{“ key”:“ value”,“ key”:“ value”}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.