通过iphone中的json webservices发布数据 [英] Post Data through json webservices in iphone
问题描述
我想通过json webservicesmy发布数据按钮点击操作代码是:
I want to post data through json webservicesmy on button click Action code is:
-(IBAction)login:(id)sender {
NSString *newurlString = [NSString StringWithFormat:@"{\"name\":\"asit\",\"email\":\"m.kulkarni@easternenterprise.com\",\"businessType\":\"1\",\"score\":30}"];
NSString * url = @"http://www.nieuwe-dag.nl/mobile_be/public/?action=saveScore";
NSData *postData = [newurlString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"urlString::%@",newurlString);
NSLog(@"postLength::%@",postLength);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
if( theConnection ) {
webData = [[NSMutableData data] retain];
}
else {
NSLog(@"theConnection is NULL");
}
}
并且控制台上的响应是:
[会话开始于2011-07-25 12:36:48 +0530。] 2011-07-25
12:36:48.860 DepartmentalStoreAdmin [2490:207]无法加载带有标识符
com.easternenterprise.dag2011-07-的包中nib引用的
header.png图像25 12:36:48.864
DepartmentalStoreAdmin [2490:207]无法加载带有标识符
com.easternenterprise.dag的包中nib引用的leher.png图像
2011-07-25 12:36:50.708
DepartmentalStoreAdmin [2490:207]
urlString :: {name:asit,email:m.kulkarni@easternenterprise.com, businessType:1,得分:30}
2011-07-25 12:36:50.709 DepartmentalStoreAdmin [2490:207]
postLength :: 88 2011-07-25 12:36 :52.376
DepartmentalStoreAdmin [2490:207]完成。收到的字节数:52 2011-07-25
12:36:52.377 DepartmentalStoreAdmin [2490:207]
[Session started at 2011-07-25 12:36:48 +0530.] 2011-07-25 12:36:48.860 DepartmentalStoreAdmin[2490:207] Could not load the "header.png" image referenced from a nib in the bundle with identifier "com.easternenterprise.dag" 2011-07-25 12:36:48.864 DepartmentalStoreAdmin[2490:207] Could not load the "leher.png" image referenced from a nib in the bundle with identifier "com.easternenterprise.dag" 2011-07-25 12:36:50.708 DepartmentalStoreAdmin[2490:207] urlString::{"name":"asit","email":"m.kulkarni@easternenterprise.com","businessType":"1","score":30} 2011-07-25 12:36:50.709 DepartmentalStoreAdmin[2490:207] postLength::88 2011-07-25 12:36:52.376 DepartmentalStoreAdmin[2490:207] DONE. Received Bytes: 52 2011-07-25 12:36:52.377 DepartmentalStoreAdmin[2490:207]
THEXML {result:ERROR,错误:[名称不能为空]}
THEXML {"result":"ERROR","errors":["Name cannot be empty"]}
我不知道它为什么不发布。请任何人帮助我非常紧急。
I dont have any idea why it is not post. Please any one help me its very urgent.
提前致谢:
推荐答案
您的 newurlString
必须包含变量名称。如果你也添加百分比,那也没关系。例如:
Your newurlString
must contain the variable name. It will be OK if you add percentage engoding too. For example:
NSString *newUrlString = [[NSString stringWithFormat:@"&json={\"name\":\"asit\",\"email\":\"m.kulkarni@easternenterprise.com\",\"businessType\":\"1\",\"score\":30}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
如果您没有看到任何更改,请点击此处:
If you don't see any changes, here they are:
- 在字符串的开头是
& json =
。将其更改为您的真实变量名称。例如。如果您的服务器在$ _ POST ['data']
中检查json,则应使用& data =
。 - 有
stringByAddingPercentEscapesUsingEncoding
,它增加了百分比转义(例如空格
转换为%20
)
- Thre is
&json=
at the begining of the string. Change it to your real variable name. E.g. if your server checks for json in$_POST['data']
, you should use&data=
. - There is
stringByAddingPercentEscapesUsingEncoding
which adds percent escapes (e.gspace
is converted to%20
)
编辑:对不起,这是正确的代码:
Sorry, here is the proper code:
NSString *newUrlString = [NSString stringWithFormat:@"&json=%@",[[NSString stringWithFormat:@"{\"name\":\"asit\",\"email\":\"m.kulkarni@easternenterprise.com\",\"businessType\":\"1\",\"score\":30}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
这篇关于通过iphone中的json webservices发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!