这是我的 php url,在发送正确的参数值后显示“true”。
http://teknofolk.com/spisrett_admin/slave/signup.php?username=something&password=pass&request_type=2
如果我发送不正确的数据,它会显示“false”
这是我从我的应用程序发送数据的代码:
-(void)PostLoginData:(NSString *)userEmail :(NSString *)Password{
NSDictionary *params = @{
@"username":userEmail,
@"password":Password,
@"request_type":@"2"
};
/* We iterate the dictionary now
and append each pair to an array
formatted like <KEY>=<VALUE> */
NSMutableArray *pairs = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *key in params) {
[pairs addObject:[NSString stringWithFormat:@"%@=%@", key, params[key]]];
}
/* We finally join the pairs of our array
using the '&' */
NSString *requestParams = [pairs componentsJoinedByString:@"&"];
NSData *postData = [requestParams dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://teknofolk.com/spisrett_admin/slave/signup.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if( theConnection ){
// indicator.hidden = NO;
NSMutableData *mutableData = [[NSMutableData alloc]init];
}
}
这是代表:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
__responseData = [[NSMutableData alloc] init];
}
我如何获得从我的服务返回的值“true”或“false”?
最佳答案
同样使用下面的委托(delegate)方法:
-(void) connection:(NSURLConnection *) connectiondidReceiveResponse:(NSURLResponse *) response
{
if(!datWebServiceData)
datWebServiceData =[[NSMutableData alloc]init];
[datWebServiceData setLength: 0];
}
-(void) connection:(NSURLConnection *) connectiondidReceiveData:(NSData *) data
{
[datWebServiceData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSString *theXML = [[NSString alloc] initWithData:datWebServiceData encoding:NSUTF8StringEncoding];
//To check for the true or False
if ([theXML rangeOfString:@"True"].location == NSNotFound)//if false
{
//If it false
}
else//if it is true
{
//it is true
}
}
关于ios - 从 URL 获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628833/