如何解决如何通过iphone的控制台检查我的数据是否保存在Web服务器api上
|| 我有一个应用程序,通过在“ 0”中输入值并通过服务器获取响应,将数据发送到Web服务器API。我编写了以下代码以将数据发送到服务器。我想从服务器接收JSON格式的数据。 这是我的.h文件:#import <UIKit/UIKit.h>
@interface apicontroller : UIViewController
{
IBOutlet UITextField *txtfirstName;
IBOutlet UITextField *txtlast;
IBOutlet UITextField *txtUserName;
IBOutlet UITextField *txtPassword;
IBOutlet UITextField *txtEmail;
NSMutableData *webData;
}
@property (nonatomic,retain)UITextField *txtUserName;
@property(nonatomic,retain)UITextField *txtPassword;
@property (nonatomic,retain)UITextField *txtfirstName;
@property(nonatomic,retain)UITextField *txtlast;
@property(nonatomic,retain)UITextField *txtEmail;
@property(nonatomic,retain)NSMutableData *webData;
-(IBAction)click:(id)sender;
@end
这是我的.m文件:
#import \"apicontroller.h\"
@implementation apicontroller
-(void)sendRequest
{
NSString *post = [NSString stringWithFormat:@\"key1=%@&key2=%@&key3=%@&key4=%@&key5=%@\",txtfirstName.text,txtlast.text,txtUserName.text,txtPassword.text,txtEmail.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@\"%d\",[postData length]];
NSLog(@\"%@\",postLength);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@\"http://192.168.0.68:91/JourneyMapperAPI?RequestType=Register&Command=NEW\"]];
[request setHTTPMethod:@\"POST\"];
[request setValue:postLength forHTTPHeaderField:@\"Content-Length\"];
[request setValue:@\"application/x-www-form-urlencoded\" forHTTPHeaderField:@\"Content-Type\"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(@\"%@\",webData);
}
else
{
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@\"%@\",loginStatus);
//greeting.text = loginStatus;
[loginStatus release];
[connection release];
[webData release];
}
-(IBAction)click:(id)sender
{
NSString *first = txtfirstName.text;
NSString *last = txtlast.text;
NSString *user = txtUserName.text;
NSString *pass = txtPassword.text;
NSString *email = txtEmail.text;
[self sendRequest];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[txtfirstName resignFirstResponder];
[txtlast resignFirstResponder];
[txtUserName resignFirstResponder];
[txtPassword resignFirstResponder];
[txtEmail resignFirstResponder];
}
- (void)dealloc {
[super dealloc];
}
@end
当我运行此代码并在ѭ3中输入值并单击按钮时,在控制台上将收到以下响应:
{\“ isError \”:true,\“ ErrorMessage \”:\\“名字为必填项。姓氏为必填项。密码为必填项。电子邮件为必填项。用户名为必填项。”,“ \” Result \“:null, \“ ErrorCode \”:903}
但是我已经将所有参数传递给了api。
请帮助我解决这个问题。
谢谢
解决方法
如果我看您的最后一个问题,则您的服务器似乎期望URL中的所有数据:
http://192.168.0.68:91/JourneyMapperAPI?RequestType=Register&Command=NEW&firstname=rocky&lastname=singh&Username=rocky14&Password=xxx&Email=rocky@gmail.com
但是,您会在URL中发送部分请求,并在HTTP正文中将请求的一部分作为URL编码的数据发送。
此外,您使用非常通用的名称key1,key2,key3等,而不是特定的名称(名,姓,用户名,密码等)。
总体而言,问题很可能是您未按照服务器期望的格式发送请求。如果没有服务器API的描述,很难在代码中真正找出问题所在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。