gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:22:18 25 4
gpt4 key购买 nike

我正在开发一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com