嗨我正在尝试将json请求传递给server.I已经使用了api,我必须在我的api中传递json参数但是我不知道如何在api中传递json参数知道我正在使用AFNetworking用于传递json参数,下面是我使用过的代码。请帮我解决这个问题,提前谢谢..
参数格式类似于
{
"email" : "abc@gmail.com",
"phone" : "1234567890",
"password" : "aaa",
"medium" : "Email",
"name" : "abc",
"uos" : "i"
}
代码:
services *srv=[[services alloc]init];
NSString *str=@"http://emailsending.in/setting_saver_api/"; NSString *method=@"registration.php";
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:self.txtName.text forKey:@"name"];
[dict setObject:self.txtEmail.text forKey:@"email"];
[dict setObject:self.txtphone.text forKey:@"phone"];
[dict setObject:self.txtPassword.text forKey:@"password"];
[dict setObject:@"Email" forKey:@"medium"];
[dict setObject:@"i" forKey:@"uos"];
NSString *jsonString;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json string :%@",jsonString);
}
[srv postToURL:str withMethod:method andParams:jsonString completion:^(BOOL success, NSDictionary *responseObj)
{
NSLog(@"res :%@",responseObj);
NSLog(@"%d",success);
NSLog(@"Successfully..................");
}];
答案 0 :(得分:0)
这是你得到的错误。请联系您的后端团队。
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /setting_saver_api/ was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
你可以像这样使用:
NSString* jsonString = [NSString stringWithFormat:@"email=%@&phone=%@&password=%@&name=%@&uos=%@",email.text,phone.text,password.text,name.text,uos.text];//enter your own values
NSURL *urlPath = [NSURL URLWithString:@"Your URL String"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlPath cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSData *requestData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(data != nil){
//your code
}
else {
//error
}
}];
希望这有帮助。
答案 1 :(得分:0)
以下代码可能有所帮助:
NSString *comment_id = @"comment"
NSString *string = [NSString stringWithFormat:@"apiURL];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"apikey":apikey,@"text":text, @"comment_id":comment_id}; // this is an example
[manager POST:string parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
NSDictionary *data = (NSDictionary *)responseObject;
if ([[[data objectForKey:@"response"] objectForKey:@"status"] integerValue]){
//success
}else{
//else part goes here
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
答案 2 :(得分:0)
试试这种方式,
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *params = @ {@"user" :txtUserName, @"pwd" :txtPwd };
[manager POST:URL_SIGNIN parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];