Ios 在GET方法中将Json值作为参数发送时不支持URL
我尝试通过get方法发送json参数。它给出的错误为Ios 在GET方法中将Json值作为参数发送时不支持URL,ios,json,xcode6,Ios,Json,Xcode6,我尝试通过get方法发送json参数。它给出的错误为“unsupported url(-1002)” 当我与邮递员核对时,URL运行良好。我无法找出问题所在 我哪里出错了?我认为问题在于如何在NSURL中编码NSDictionary 您可能希望您的URL如下所示:http://my domain.com/agentuserlist?loc=Chennai。但是NSURL中NSDictionary的原始编码不会产生此结果 你可以遵循公认的答案 从这个问题了解如何将NSDictionary转换为UR
“unsupported url(-1002)”
当我与邮递员核对时,URL运行良好。我无法找出问题所在
我哪里出错了?我认为问题在于如何在
NSURL
中编码NSDictionary
您可能希望您的URL如下所示:http://my domain.com/agentuserlist?loc=Chennai
。但是NSURL
中NSDictionary
的原始编码不会产生此结果
你可以遵循公认的答案
从这个问题了解如何将
NSDictionary
转换为URL参数的常规列表(使用字典值的正确编码:不要忘记stringByAddingPercentEscapeUsingEncoding
部分):您能写出请求的内容吗?您在创建NSURL
实例或尝试发送请求时是否出现错误?我收到的响应为不受支持的URL您想要整个URL吗?只需记录您的strUrlName,我想它不正确!像这样构建URL是完全不正确的。你不能仅仅将字典stringWithFormat
转换成URL(即使你对它进行了%转义)并期望它能工作。您必须首先确认web服务需要什么,然后才能开始构建请求。我怀疑类似Romain的答案是可能的,但不知道这个filters=
的组合在哪里(我想你是从文档中的某个地方得到的)。但是,如果我们试图告诉您如何解决这个问题,而没有对web服务的期望提供明确的指导,这是很愚蠢的。
Code:
arrValues = [[NSMutableArray alloc]initWithObjects:@"Chennai", nil];
arrKeys = [[NSMutableArray alloc]initWithObjects:@"loc", nil];
dicValue = [NSDictionary dictionaryWithObjects:arrValues forKeys:arrKeys];
NSString *strMethodName = @"agentuserlist";
strUrlName = [NSString stringWithFormat:@"%@%@?filters=%@",appDelegate.strURL, strMethodName, dicValue];
//strUrlName = [strUrlName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:strUrlName] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:250.0];
[request setHTTPMethod:@"GET"];
[request setHTTPShouldHandleCookies:YES];
[request setValue:@"zyt45HuJ70oPpWl7" forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSError *error;
NSURLResponse *response;
NSData *received = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
NSLog(@"error:%@", error);
NSString *strResponse = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
NSLog(@"response :%@", strResponse);