views:

73

answers:

3

Hi to All,

I have used below code in my application of User Registration to interact with remote database.

-(IBAction)checkFirstName:(UITextField*)textField   
{

    NSURL  *url = [NSURL URLWithString:@"http://mysite.com"];

     ASIFormDataRequest  *request = [ASIFormDataRequest  requestWithURL:url];
     [request setPostValue: firstName.text  forKey:@"firstName"]; 

     [request  setDelegate:self];
     [request  didReceiveDataSelector];
     [request  startAsynchronous];

      firstName.rightViewMode = UITextFieldViewModeAlways;
       if (receivedData==0)
      { 
        UIImage  *image = [UIImage imageNamed:@"close.png"];
        [button  setImage:image  forState:UIControlStateNormal];
        firstName.rightView = button;
      }
}

'firstName' is the textfield value which i used to take input from user. I used the below method to receive response from database

 -(void)request:(ASIHTTPRequest *)request  didReceiveData:(NSData *)data

    {
      receivedData = [request responseData];
     [receivedData  appendData:data];

     receivedString = [[NSString alloc]  initWithData:receivedData
               encoding:NSUTF8StringEncoding];
    }

'receivedData' is the NSData object, declared globally. I have to get the 'responseData' as either 0 or 1, which will be stored in a variable 'receivedData'.

  1. How to check the value of 'receivedData' object (0 or 1)?.
  2. checkFirstName: action will be fired on uitextfield Editing DidEnd event.

Can we use responseData==0 ? Please, suggest me with a solution... Waiting for an urgent response..

A: 

using (responseData == 0) will only tell you if responseData is allocated or not.

If I understand what you said, you need to check the response you got from the web...

In that case after :

receivedString = [[NSString alloc]  initWithData:receivedData
           encoding:NSUTF8StringEncoding];

Do a simple NSLog :

NSLog(@"result: %@", receivedString);
TheSquad
@TheSquad, i checked with NSLog, but didReceiveData: method was not getting called. Thanks for reply.
A: 

to begin with, it looks to me as if you're checking the result of the request immediately, but being an asynvhronous request, the check will happen before the request is finished.

As for your main question, if the server responds with one character (just '0' or '1') you might be better off using [request responseString] directly, as it will automagically detect the enconding and do the conversion for you.

You cannot compare receivedData with 0 or 1, as it is a pointer and you won't get the expected results.

Victor Jalencas
Jalences,Thanks for your help.. what you said is Right. But, where i have to use: [request responseString]?Can i directly compare this 'responseString' with 0 or 1? how can i repeat this check for multiple textfields of my application?waiting for quick response...
yes, you can compare the response string multiple times but... as kubi posted, that probably isn't what you need. Why would you compare the same response to multiple text fields? I cannot imagine what lead you to this design, please expand on what your app needs to do.
Victor Jalencas
My app is similar to Twitter signUp form. In this,when new user gives his details in the fields provided, it shows the availability of them, just next to the entered value. So, my requirement is: when i use [request responseString], it is returning NULL value. Is it possible using simulator? If possible, how can i retrieve the response(integer value) from remote URL using ASIHTTPRequest? how to repeat this for all input fields in my app? Thank you.
then you wouldneed to issue a new request for every field, as different fields would have different availabilities even for the same values. If you issue async requests, on each requestFinished delegate method that you implement (each field should have separate delegate methods) you update the label next to the field
Victor Jalencas
+2  A: 

First, you are going to need to rethink the way your methods work.

  1. [request didReceiveDataSelector]; returns the method name that is called when the request receives data. Since you are not setting this value to a variable, this line does nothing.
  2. if (receivedData==0) won't do what you want it to do. The request runs asynchronously, so the value of receivedData will not ever reflect what happens in the request you started 2 lines previously.
  3. Are you trying to check if receivedData is null? If so, say this: if (receivedData == nil), it makes it clear what you're trying to do here.
  4. In your request:didReceiveData method it looks like you are getting the data from the request and appending it onto itself (I'm not sure what the responseData property looks like when you access it before the request is finished. It could be null). Whatever is happening here, I'm pretty sure it's not what you wanted to happen.

Finally, unless you really want to aggregate and parse the incoming data yourself (you probably don't) you should impliment these two methods instead of the request:didReceiveData method.

- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;

They're called after the request is completely done. Access a string value for the server response by calling

NSString *receivedString = [request responseString];

Only after your requestFinished method is called can you set the value of your first name UITextField.

And one bit of advice: it's rare that you need to use global variables in Obj-C and you certainly do not need to use them here. I'd get rid of those ASAP.

I made a lot of assumptions about what your intent was, so leave a comment if this isn't what you wanted. Good luck!

kubi
@Kubi, Thank you so much for giving reply. I got cleared on this.- (void)requestFinished:(ASIHTTPRequest *)request{ NSString *receivedString = [response responseString];}I got null string.. can i include any more,to receive the response?How to get back to textField method to compare the response with 0 or 1? Reply please..
I'm sorry, that was a mistake in my answer. It should be `[request responseString];`
kubi
And also, what are you expecting back from your request? Just a 1 or a 0, or are you just trying to determine if there is any response at all?
kubi
Kubi, have a look at this line in your reply:- NSString *receivedString=[response responseString];replace 'response' with 'request' object.Correct me, if i was wrong. Post a reply to my previous comment also..
Kubi, i used NSURL class to receive response from remote server.I have done with it, for one value(of my app, signUp form). My question is: How to call the method "didReceiveData" of NSURLConnection , for multiple textfields.Please help me.
I'm not sure what you're asking. Unless you're doing something quite different than what I suggested in my answer, you shouldn't ever call or implement `didReceiveData`.
kubi
kubi,Thanks for all your help.