2015-03-25 39 views
0

這是我的JSON字典如何調用在IOS PUT請求,並需要通過一個字典

{"pid":"14982","type":"intervention","uid":"10008","bookmark_g7l03":{"und":[{"value":"S:1","format":"null","safe_value":"S:1"}]}} 

我需要PUT請求傳遞到以下網址

http://example.com/services/profiles/pid 

讓我們知道如何要通過字典來Webservice的URL在IOS

NSString *data = [NSString stringWithFormat:@"{\"pid\":\"%@\",\"type\":\"%@\",\"uid\":\"%@\",\"%@\":{\"und\":\[{\"value\":\"%@\",\"format\":\"null\",\"safe_value\":\"\%@\"}]}}",pid,type, uidNo,bkMarkStr,self.startString,self.startString]; 
NSURL *bkMrkUrl = [NSURL URLWithString:@"http://example.com/services/profiles/pid=14997"]; 
NSData *postData = [data dataUsingEncoding:NSUTF8StringEncoding]; 
NSMutableURLRequest *bkMrkReq = [[NSMutableURLRequest alloc]initWithURL:bkMrkUrl]; 
[bkMrkReq setHTTPMethod:@"PUT"]; 
[bkMrkReq setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[bkMrkReq setHTTPBody:postData]; 
[NSURLConnection sendAsynchronousRequest:bkMrkReq queue:[NSOperationQueue currentQueue] 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 


           NSString *txt = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

           NSLog(@"data....:%@",txt); 

           // handle response here 
          }]; 

在這裏,當我打印的文本數據

輸出是:<?xml version="1.0" encoding="utf-8"?> <result>CSRF validation failed</result>

我應該做的與數據.. 這裏正在更新在服務器領域的信息。

+0

結帳AFNetworking http://afnetworking.com/ – user623396 2015-03-25 08:50:07

回答

1

你可以試試:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://example.com/services/profiles/pid"]]; 
[request setHTTPMethod:@"PUT"]; 

NSString *params = @"\{\"pid\" : \"14997\", \"type\" : \"intervention\", \"uid\" : \"10046\"}"; // The rest of your parmas here 

[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
          // handle response here 
         }]; 

如果你想請求同步,而不是你可以使用:

NSURLResponse *response; 
NSError *error; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
+0

@Olllie,我們如何可以看到回覆 – 2015-03-25 10:22:47

+0

我正在獲取數據..,讓我們知道如何使用該數據 – 2015-03-25 10:24:59

+0

你知道數據的格式嗎?如果是JSON,你可以使用:'NSDictionary * json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];' – Ollie 2015-03-25 10:46:12