ios - 从 URL 获取响应

标签 ios objective-c ios7

这是我的 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/

相关文章:

objective-c - 比较不同的 'the launching Cocoa app with parameters' 方法

ios - 如何从 sqlite 查询中的 NSString 中删除 '

ios - 从数组中删除 - fatal error : Index out of range - SwiftUI Binding

ios - 如何解决这个错误? - 类 'ViewController' 没有初始化器

c - 如何通过 12 的幂来提高 double 值?

ios - 使用 UITextView 占位符并且在重新输入时不清除文本

ios - UIView 和 intrinsicContentSize

ios - UIView 动画立即发生

objective-c - 将 subview 添加到背景/堆栈背面?

ios - 在 iOS 7 中重用 UIPickerView 中的 View