首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iphone json web-services发布数据

iphone json web-services发布数据
EN

Stack Overflow用户
提问于 2013-11-15 06:24:45
回答 1查看 3.4K关注 0票数 0

我正在开发一个iOS应用程序,在该应用程序中,我需要将json数据发布到web服务url & base中,我需要从服务器端获得响应。

由于我试图用json格式发送web数据,但不幸的是,我没有得到任何帮助来实现这一点。

下面是web-url & json请求数据,我需要在web服务上传递这些数据才能得到响应,

WebUrl:ios

网络数据传递:webdata={"device_token":"test"}

现在,我很困惑如何通过webdata={"device_token":"test"} json从服务器端获得响应。

下面是我的代码,我很想让它成为可能,

编码:

代码语言:javascript
复制
  NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    NSURL *postURL = [NSURL URLWithString: @"http://testing.com/controllogic/webservices/all_data_webservice/set_device_token_ios"];
    NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Akash IOS PUSH", @"device_token",
                              nil];

   // NSString *string_val = @"webdata=";

   // NSString *data = [NSString stringWithFormat:@"data=%@",[[NSString alloc] initWithData:jsData                                                                                 encoding:NSUTF8StringEncoding]];

   // NSString *myString_VAL =[NSString stringWithFormat:@"%@%@",string_val,jsonDict];

    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: postURL
                                                           cachePolicy: NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval: 60.0];

    [request setHTTPMethod: @"POST"];
    [request setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"Accept"];
    [request setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField: @"content-type"];

    [request setHTTPBody: jsonData];

    [NSURLConnection sendAsynchronousRequest: request
                                       queue: queue
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
                               if (error || !data) {
                                   // Handle the error

                                   NSLog(@"Server Error : %@", error);
                               } else {
                                   // Handle the success

                                   NSLog(@"Server Responce :%@",response);
                               }
                           }
     ];

请任何人帮助我让这一切成为可能。

EN

回答 1

Stack Overflow用户

发布于 2013-11-15 06:45:10

你可以用下面的代码,我希望这可能有用。

代码语言:javascript
复制
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"Akash IOS      PUSH", @"device_token",nil];


NSData * JsonData =[NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:nil];
NSString * jsonString= [[NSString alloc] initWithData:JsonData encoding:NSUTF8StringEncoding];



NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"%@",jsonString] dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://testing.com/controllogic/webservices/all_data_webservice/set_device_token_ios"]]];
[request setHTTPBody:body];
[request setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest: request
                                   queue: queue
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
                           if (error || !data) {
                               // Handle the error

                               NSLog(@"Server Error : %@", error);
                           } else {
                               // Handle the success

                               NSLog(@"Server Response :%@",response);
                           }
                       }
 ];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19994794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档