【问题标题】:URL for GET requestGET 请求的 URL
【发布时间】:2013-02-05 07:05:53
【问题描述】:

为了获得 JSON,我使用了这个答案 SBJsonWriter Nested NSDictionary

现在我有一个刺痛{"key1":"bla1","key2":{"a":"a1","b":"b1","c":"c1","d":"d1"},"key3":"bla3"},我称之为theString 我需要将它添加到一个 url http://mysyte.net:8888/JSON? 并收到这样的东西http://lcwebtest.sytes.net:8888/JSON?{"key1":"bla1","key2":{"a":"a1","b":"b1","c":"c1","d":"d1"},"key3":"bla3"}

这是我的工作: NSString *urlString = [NSString stringWithFormat:@"http://mysyte.net:8888/JSON?%@",theString];

NSLog 给出http://mysyte.net:8888/JSON?{"key2":{"d":"d1","b":"b1","c":"c1","a":"a1"},"key1":"bla1","key3":"bla3"}

然后我通过它创建一个网址 NSURL *url1 = [NSURL URLWithString:urlString];

但是NSLog(@"%@",url1); 给了我{null}

我假设 NSURL 不想读取“{”或“}”,并认为 url 格式错误。

如何接收 url 以发出 GET 请求?

【问题讨论】:

    标签: ios json get


    【解决方案1】:

    我假设 NSURL 不想读取“{”或“}”,并认为 url 格式错误。

    没错,您不能在 URL 中放置一些特殊字符。您想像这样转义 JSON 字符串:

    CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(
        NULL,
        (CFStringRef)theString, // (__bridge CFStringRef)theString if you use ARC
        CFSTR(""),
        CFSTR("?&=%,:+-"),
        kCFStringEncodingUTF8
    );
    NSString *urlString = [NSString stringWithFormat:@"http://mysyte.net:8888/JSON?%@", (NSString *)escaped];
    CFRelease(escaped);
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 2021-08-01
      • 2013-03-10
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      相关资源
      最近更新 更多