sohel14_cse_ju
sohel14_cse_ju

Reputation: 2521

Getting Response from URL

Here is my php url which shows "true" after sending proper parameter value.

 http://teknofolk.com/spisrett_admin/slave/signup.php?username=something&password=pass&request_type=2

If i send incorrect data it shows "false"

Here is my Code for sending data from my app :

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

And here is the delegate :

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

How can i get the value "true" or "false" returning from my Service?

Upvotes: 0

Views: 59

Answers (2)

nst
nst

Reputation: 3882

If you want to simplify your code, you can use a simple NSURLConnection wrapper such as 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];

Upvotes: 0

Suyog Patil
Suyog Patil

Reputation: 1620

Use the following delegate method for the same:

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

Upvotes: 1

Related Questions