【问题标题】:NSURLRequest + JSON : Bad URLNSURLRequest + JSON:错误的 URL
【发布时间】:2013-09-17 13:38:37
【问题描述】:

我是 iOS 开发新手。

我正在使用以下代码向服务器发送请求并同步获取响应。

我的请求包含 JSON 对象。当我在模拟器上运行时,出现错误“Bad URL”。

请帮我解决这个问题。

NSString *regUrlWithJson = @"https://dso.mpl.com/reg?json={\"operation\":\"login\",\"contactEmail\":\"abc@rt.com\",\"password\":\"e3dba1bbfab020a2eb105c69405a7766\",\"SSO\":\"true\",\"service\":\"lp\",\"sig\":\"true\",\"url\":\"mpl\"}";

NSLog(regUrlWithJson);
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:regUrlWithJson]];

NSURLResponse * response = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
                                      returningResponse:&response
                                                  error:&error];

if (error != nil)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}
else{
    NSString *string = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:string delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}

谢谢,

【问题讨论】:

  • 得到了答案。需要对 URL 进行编码。 NSURL *theUrl = [NSURL URLWithString: [regUrlWithJson stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURLRequest * urlRequest = [NSURLRequest requestWithURL:theUrl];

标签: json nsurlrequest


【解决方案1】:

regUrlWithJson 不是有效的 URL。这就是原因

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多