2012-04-10 61 views
0

我正在創建一個iPhone應用程序,並試圖找出如何創建一個包含參數的webservice的JSON請求。在Java中,這將是這樣的如何使用Objective-C中的參數發送JSON請求到服務

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); // [email protected] 
      json.put("md5pw", password); // donkeykong 

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

標題必須包含一個JSON對象和「X-FBR-應用」的服務識別請求之前。 Objective-C如何實現?

回答

1

使用後method.try這

NSString *method = @"addProduct"; 
NSString *[email protected]"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); 

可以追加這樣所有的參數。

+0

感謝您的快速響應。到目前爲止我有一個問題。在您發送的代碼中,我的內容類型是 - >「X-FBR-App」? – Lahib 2012-04-10 12:22:21

+0

嘗試用「X-FBR-App」替換內容類型 – 2012-04-10 12:29:49

+0

請參閱http://stackoverflow.com/questions/4085978/json-post-request-on-the-iphone-using-https – 2012-04-10 12:42:04

1

JSON並未納入iOS,但如果您查看www.JSON.org,有許多Objective-C框架可提供所需的功能。我沒有任何經驗,所以我不能提出建議,但尋找解決方案是一個好的開始。

也看看這個SO問題post-data-through-json-webservices-in-iphone

+0

從iOS 5.0和OS X 10.7開始JSON被烘焙到Cocoa中http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html – JeremyP 2012-04-10 14:13:35

+0

@JeremyP很好,讓我看起來愚蠢的..謝謝! – 2012-04-10 14:21:15

相關問題