我正在开发一个 iOS 应用程序,其中我需要将 json web 数据发布到 web 服务 url 和我需要从服务器端获得响应的基础。
因为我尝试编写代码以使用 json 格式发送网络数据,但不幸的是,我没有任何帮助来实现这一点。
下面是我需要传递给网络服务以获得响应的网络 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);
}
}
];
请任何机构帮助我使这成为可能。