gpt4 book ai didi

ios - 如何在 iOS 中使用 post 方法通过传递参数来创建注册表单?

转载 作者:行者123 更新时间:2023-11-29 01:20:31 26 4
gpt4 key购买 nike

我正在尝试对 Api 执行 POST 请求。这个想法是用参数发出请求:姓名,电子邮件,手机号码,密码。问题是服务器无法识别这些参数。我确实得到了“成功”作为回应。这是我的代码

-(NSData *)newUser:(NSString *)newUserName andMobNo:(NSString *)mobileNo andEmail:(NSString *)emailId andimei:(NSString *)imei andPassword:(NSString *)pswd andSESSION_ACCESS_KEY:(NSString *)accessKey andPhoto:(NSString *)photo andFbloginId:(NSString *)fbID
{
NSString *urlString = [NSString stringWithFormat:@"http://demo.tugain.com/contest/TugainAPI/setUserDetail"];
NSString *jsonString = [NSString stringWithFormat:NEWSIGNUP,newUserName,mobileNo,emailId,imei,pswd,accessKey,photo,fbID];
NSData *myJSONData =[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:myJSONData]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
return returnData;
}

这是我的注册方法

-(void)signUpAction:(id)sender
{
MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.view];
NSString *str=@"User";
[hud showWhileExecuting:@selector(callingMethod:) onTarget:self withObject:str animated:YES];
}

-(void)callingMethod :(NSString *)str
{
[self performSelectorOnMainThread:@selector(dataLoadMethod:) withObject:str waitUntilDone:NO];
}

-(void)dataLoadMethod :(NSString *)strCon
{
NSString *photo =@"NO_PIC";
NSString *fbID =@"0";
NSString *accessId = @"akjsda9s8dadandlakd9";
NSString *imei = @"34343434";

if([strCon isEqualToString:@"User"])
{
NSData *returnData;
if(self.regEmailTxtFld.text.length>0)
{
BOOL val=[self validEmail:self.regEmailTxtFld.text];
if(val)
{
returnData =[[WebServices alloc] newUser:regFullNameTxtFld.text andMobNo:regMbleTxtFld.text andEmail:regEmailTxtFld.text andimei:imei andPassword:regPswdTxtFld.text andSESSION_ACCESS_KEY:accessId andPhoto:photo andFbloginId:fbID];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Attention" message:@"Please Provide a Valid Mail-id" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert show];
[self performSelector:@selector(dismissView:) withObject:alert afterDelay:2.0];
}
}

if(returnData.length > 0)
{
NSError *error;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
NSLog(@"New Customer SignUp resp is %@",res);
}
else //if data is null
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Attention" message:@"Error in Response" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert show];
[self performSelector:@selector(dismissView:) withObject:alert afterDelay:3.0];
}
}

我正在传递照片、fbid、accessId、imli

最佳答案

我认为问题在于准备 HTTPBody。根据我的经验,在创建 application/json 类型请求时,我总是先创建一个正确的 NSDictionary,然后使用 NSJSONSerialization 方法对其进行序列化.

使用您的代码,它看起来像:

-(NSData *)newUser:(NSString *)newUserName andMobNo:(NSString *)mobileNo andEmail:(NSString *)emailId andimei:(NSString *)imei andPassword:(NSString *)pswd andSESSION_ACCESS_KEY:(NSString *)accessKey andPhoto:(NSString *)photo andFbloginId:(NSString *)fbID
{

NSString *urlString = [NSString stringWithFormat:@"http://demo.tugain.com/contest/TugainAPI/setUserDetail"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSDictionary *jsonObject = @{
@"userName-key" : newUserName,
@"mobileNo-key" : mobileNo,
@"emailId-key" : emailId,
@"imei-key" : imei,
@"pswd-key" : pswd,
@"accessKey-key": accessKey,
@"photo-key" : photo,
@"fbID-key" : fbID
};
NSData *httpBody = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:nil];
[request setHTTPBody:httpBody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
return returnData;
}

如果这是解决方案,请记住将字典键更改为正确的值,并在将其放入字典之前检查每个值是否存在,否则会崩溃。

关于ios - 如何在 iOS 中使用 post 方法通过传递参数来创建注册表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34654364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com