我正在开发一个iOS应用程序,在该应用程序中,我需要将json数据发布到web服务url & base中,我需要从服务器端获得响应。
由于我试图用json格式发送web数据,但不幸的是,我没有得到任何帮助来实现这一点。
下面是web-url & json请求数据,我需要在web服务上传递这些数据才能得到响应,
WebUrl:ios
网络数据传递:webdata={"device_token":"test"}
现在,我很困惑如何通过webdata={"device_token":"test"} json从服务器端获得响应。
下面是我的代码,我很想让它成为可能,
编码:
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);
}
}
];请任何人帮助我让这一切成为可能。
发布于 2013-11-15 06:45:10
你可以用下面的代码,我希望这可能有用。
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);
}
}
];https://stackoverflow.com/questions/19994794
复制相似问题