如何显示ASIHTTPRequest的responseData的值?
我在用户注册应用程序中使用了以下代码来与远程数据库进行交互。
-(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”是我用来获取用户输入的文本字段值。 我使用下面的方法从数据库接收响应
-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
{
receivedData = [request responseData];
[receivedData appendData:data];
receivedString = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];
}
“receivedData”是全局声明的 NSData 对象。我必须将“responseData”获取为 0 或 1,它将存储在变量“receivedData”中。
- 如何检查“receivedData”对象的值(0 或 1)?
- checkFirstName:将在 uitextfield 编辑 DidEnd 事件上触发操作。
我们可以使用responseData==0吗?请给我建议一个解决方案... 等待紧急回复..
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'.
- 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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您需要重新思考您的方法的工作方式。
[request didReceiveDataSelector];
返回请求接收数据时调用的方法名称。由于您没有将此值设置为变量,因此该行不执行任何操作。if (receivedData==0)
不会做你想要它做的事情。该请求异步运行,因此receivedData
的值永远不会反映您之前启动的 2 行请求中发生的情况。receivedData
是否为空?如果是这样,请这样说:if (receivedData == nil)
,它清楚地表明您要在这里做什么。最后,除非您确实想自己聚合和解析传入数据(您可能不想),否则您应该实现这两个方法,而不是
request:didReceiveData
方法。他们在请求完全完成后被调用。通过调用访问服务器响应的字符串值。
只有在调用
requestFinished
方法后,您才能设置名字UITextField
的值。一点建议:您很少需要在 Obj-C 中使用全局变量,并且您当然不需要在这里使用它们。我会尽快摆脱那些。
我对你的意图做了很多假设,所以如果这不是你想要的,请留下评论。祝你好运!
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.receivedData
is null? If so, say this:if (receivedData == nil)
, it makes it clear what you're trying to do here.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.They're called after the request is completely done. Access a string value for the server response by calling
Only after your
requestFinished
method is called can you set the value of your first nameUITextField
.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) 只会告诉您responseData是否已分配。
如果我理解你所说的,你需要检查你从网络上得到的响应......
在这种情况下:
做一个简单的 NSLog :
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 :
Do a simple NSLog :
首先,在我看来,您似乎正在立即检查请求的结果,但作为异步请求,检查将在请求完成之前进行。
至于您的主要问题,如果服务器响应一个字符(仅
'0'
或'1'
),您最好使用[request responseString]直接
,因为它会自动检测编码并为您进行转换。您不能将
receivedData
与 0 或 1 进行比较,因为它是一个指针,您将不会得到预期的结果。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.