【问题标题】:How do I make HTTP post request for getting JSON object in response for iPhone application?如何发出 HTTP 发布请求以获取 JSON 对象以响应 iPhone 应用程序?
【发布时间】:2008-11-05 01:41:30
【问题描述】:

我有一个在服务器上运行的 Web 服务,它以 XML 格式或 JSON 格式返回数据。 我想请求 JSON 格式,但使用的是 HTTP Post 方法。

非常感谢任何帮助。

提前致谢。

【问题讨论】:

    标签: iphone json cocoa-touch nsurlrequest


    【解决方案1】:

    这是适用于 JSON 发布请求的代码, TouchJSON 框架用于解析 JSON,感谢 'schwa'。

    NSArray *keys = [NSArray arrayWithObjects:@"username", @"password", @"preference", @"uid", nil];
    NSArray *objects = [NSArray arrayWithObjects:@"accuser", @"accpass", @"abc_region", @"100", nil];
    NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    
    NSURL *theURL = [NSURL URLWithString:@"http://url.com/request.php"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
    [theRequest setHTTPMethod:@"POST"];
    
    [theRequest setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
    NSString *theBodyString = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary];
    NSLog(@"%@", theBodyString);
    NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
    // NSLog(@"%@", theBodyData);
    [theRequest setHTTPBody:theBodyData];
    
    NSURLResponse *theResponse = NULL;
    NSError *theError = NULL;
    NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
    NSString *theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(theResponseString);
    NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseString];
    NSLog(@"%@", theResponseDictionary);
    NSString *theGreeting = [theResponseDictionary objectForKey:@"greeting"];
    [self setValue:theGreeting forKey:@"greeting"];
    

    【讨论】:

    • “otherRequest”字典的目的是什么?好像没用过。
    【解决方案2】:

    不太确定您的问题到底是什么。但是谷歌“TouchJSON”应该可以帮助你入门。

    【讨论】:

    • 如何使用 TouchJSON 来做到这一点?
    【解决方案3】:

    对于错误和内存泄漏,我们深表歉意,但是如何:

    CFURLRef url = CFURLCreateWithString(NULL, CFSTR("http://example.com/post"), NULL);
    CFHTTPMessageRef msg = CFHTTPMessageCreateRequest(
        NULL,
        CFSTR("POST"),
        url,
        kCFHTTPVersion1_1);
    
    const char *body = "key=value&id=30293";
    CFDataRef bodyData = CFDataCreate(NULL, body, strlen(body));
    CFHTTPMessageSetBody(msg, bodyData);
    
    CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(NULL, myRequest);
    CFReadStreamOpen(myReadStream);
    CFHTTPMessageRef myResponse = CFReadStreamCopyProperty(
        myReadStream,
        kCFStreamPropertyHTTPResponseHeader);
    
    //
    // Handle myResponse
    //
    
    CFReadStreamClose(myReadStream);
    CFRelease(myReadStream);
    CFRelease(bodyData);
    CFRelease(msg);
    CFRelease(url);
    

    【讨论】:

    • 这里是真正的碳解决方案——在 iPhone 上根本不能很好地工作。
    • 那是核心基础,而不是 Carbon。 CFNetwork(即)在 iOS 上运行良好。我不喜欢它而不是 Cocoa 解决方案,但乍一看,我看不出这段代码有什么问题。 Core Foundation 有时非常有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2014-12-08
    • 2012-02-25
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多