【问题标题】:How to send JSON request to service with parameters in Objective-C如何使用 Objective-C 中的参数向服务发送 JSON 请求
【发布时间】:2012-04-10 11:23:19
【问题描述】:

我正在创建一个 iPhone 应用程序,并试图弄清楚如何创建对包含参数的 Web 服务的 JSON 请求。在 Java 中,这看起来像这样

HashMap<String, String> headers = new HashMap<String, String>();

JSONObject json = new JSONObject();

            json.put("nid", null);
            json.put("vocab", null);
            json.put("inturl", testoverview);
            json.put("mail", username); // something@gmail.com
            json.put("md5pw", password); // donkeykong

        headers.put("X-FBR-App", json.toString());

在服务识别请求之前,标头必须包含 JSON 对象和“X-FBR-App”。这将如何在 Objective-C 中实现?

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    使用 Post 方法。试试这个

        NSString *method =  @"addProduct"; 
     NSString *jsonstring=@"http://yourdomain.com/product_review_api/product-review.php?"; 
     NSString *urlString = [NSString stringWithFormat:@"%@",jsonstring];
     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
     [request setURL:[NSURL URLWithString:urlString]];
     [request setHTTPMethod:@"POST"]; 
      NSMutableData *body = [NSMutableData data];
        // image file
       NSData *imageData = UIImageJPEGRepresentation( labelimage, 0.8);
       NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
          //  parameter method
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
       //  parameter categoryid
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"category_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[selectedID dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [request setHTTPBody:body];
    
        // now lets make the connection to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    
        NSLog(@"%@",returnString);
        NSDictionary *profile = (NSDictionary*)[returnString JSONValue];
        NSLog(@"profile=%@", profile); 
    

    您可以像这样附加所有参数。

    【讨论】:

    • 感谢您的快速回复。到目前为止,我有一个问题。在您发送的代码中,我的 Content-Type 将是 -> "X-FBR-App" ?
    • 尝试用“X-FBR-App”替换内容类型
    • 感谢您的链接。在您发送的代码中,在这一行 ->> “NSDictionary profile = (NSDictionary)[returnString JSONValue];”我无法掌握 JSONVALUE。不知道你能不能猜到原因?
    • 你得到returnString了吗?
    【解决方案2】:

    JSON 并未融入 iOS,但如果您查看www.JSON.org,就会发现有许多提供所需功能的 Objective-C 框架。我对其中任何一个都没有经验,因此无法提出建议,但这是寻找解决方案的良好开端。

    也看看这个 SO 问题post-data-through-json-webservices-in-iphone

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2016-01-16
    • 2021-05-23
    相关资源
    最近更新 更多