如何使用get方法在具有6个值和json格式的api链接上发布数据?
我真的很费力。我这样做好几天了。是否最好有6个用于写入值的文本字段和一个用于指示发布数据的按钮?查看我的代码并请求帮助。
-(IBAction)postDataPressed
{
NSString *urlString = @"http://192.168.18.8/apisample2/friendb.php?fm=jsn";
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:user.text forKey:@"un"];
[request setPostValue:pass.text forKey:@"pd"];
[request setPostValue:gender.text forKey:@"gd"];
[request setPostValue:age.text forKey:@"ag"];
[request setPostValue:status.text forKey:@"st"];
[request setPostValue:lookfor.text forKey:@"lf"];
[request setRequestMethod:@"GET"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(@"Response: %@", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Error: %@", error.localizedDescription);
}];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
}
我希望有人会帮忙。
答案 0 :(得分:1)
NSString *request_url = [NSString stringWithFormat: @"http://192.168.18.8/apisample2/friendb.php?fm=jsn&un=%@&pd=%@&gd=%@&ag=%@&st=%@&lf=%@",
user.text, pass.text, gender.text, age.text,status.text,lookfor.text];
NSURL *url = [NSURL URLWithString:request_url];
ASIHttpRequest *request = [ASIHttpRequest requestWithURL:url];
request.delegate = self;
....
HTTP Get方法只接受url中的数据,称为查询字符串,因此您必须自己构建查询字符串。