ios - iphone json 网络服务发布数据

标签 ios json web-services

我正在开发一个 iOS 应用程序,我需要在其中将 json 网络数据发布到网络服务 url 和我需要从服务器端获得响应的基础。

因为我尝试编写代码以使用 json formate 发送网络数据,但不幸的是,我还没有得到任何帮助来实现这一点。

下面是我需要传递给 web 服务以获得响应的 web-url 和 json 请求数据,

网址:http://testing.com/controllogic/webservices/all_data_webservice/set_device_token_ios

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

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

下面是我的代码,我已经厌倦了它,

编码:

  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);
                               }
                           }
     ];

请任何人帮助我使这成为可能。

最佳答案

您可以使用下面的代码。我希望这能奏效。

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);
                           }
                       }
 ];

关于ios - iphone json 网络服务发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19994794/

相关文章:

python - 如何解析txt文件末尾的json格式文本

ASP.NET - 使用 jQuery 处理基于 JSON 的 Web 服务的正确方法是什么?

c# - Web 服务命名空间动态命名

ios - 任何应用程序在 iPhone 4 上出现黑屏和崩溃

java - 安全 - Jackson 密码序列化

ios - Storyboard静态 TableView 太多行

php - 使用 json_encode() 时如何添加空格和换行?

java - 如何将 json 字符串作为 RESTFUL 请求发送给 postman

ios - 从 InputStream 读取 UInt32

ios - RestKit POSTing multipartFormRequest 与字典参数会在数据中添加反斜杠