How to display the value of responseData of ASIHTTPRequest?
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 = [U开发者_开发百科IImage 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'.
- How to check the value of 'receivedData' object (0 or 1)?.
- 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..
First, you are going to need to rethink the way your methods work.
[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.if (receivedData==0)
won't do what you want it to do. The request runs asynchronously, so the value ofreceivedData
will not ever reflect what happens in the request you started 2 lines previously.- 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. - 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 theresponseData
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!
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);
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.
精彩评论