我有一个应用程序,通过在
textfields
中输入值并通过服务器获取响应,将数据发送到我的 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
当我运行此代码并在
textfield
中输入值并单击按钮时,我在控制台上收到以下响应:
{"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 protected]
但是您在 URL 中发送部分请求,并在 HTTP 正文中发送部分请求作为 URL 编码数据。
此外,您可以使用非常通用的名称key1、key2、key3等,而不是特定的名称(名字、姓氏、用户名、密码等)
总的来说,问题很可能是您没有以服务器期望的格式发送请求。如果没有服务器 API 的描述,就很难真正查明代码中的问题。