从URL获取响应

时间:2014-05-13 10:32:28

标签: ios objective-c ios7

这是我的php网址,显示" 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];
}

我怎样才能获得价值&#34; true&#34;或&#34;假&#34;从我的服务处返回?

2 个答案:

答案 0 :(得分:1)

使用以下委托方法:

    -(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
        }
    }

答案 1 :(得分:0)

如果您想简化代码,可以使用简单的NSURLConnection包装,例如STHTTPRequest

STHTTPRequest *r = [STHTTPRequest requestWithURLString:@"http://teknofolk.com/spisrett_admin/slave/signup.php"];

r.POSTDictionary = @{@"username":userEmail,
                     @"password":Password,
                     @"request_type":@"2"};

r.completionBlock = ^(NSDictionary *headers, NSString *body) {
    BOOL responseBodyDoesContainsTrue = [body rangeOfString:@"True"].location != NSNotFound;
};

r.errorBlock = ^(NSError *error) {
    //
};

[r startAsynchronous];