如何显示ASIHTTPRequest的responseData的值?

发布于 2024-09-13 07:56:01 字数 1334 浏览 3 评论 0原文

我在用户注册应用程序中使用了以下代码来与远程数据库进行交互。

-(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”中。

  1. 如何检查“receivedData”对象的值(0 或 1)?
  2. 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'.

  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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偷得浮生 2024-09-20 07:56:01

首先,您需要重新思考您的方法的工作方式。

  1. [request didReceiveDataSelector]; 返回请求接收数据时调用的方法名称。由于您没有将此值设置为变量,因此该行不执行任何操作。
  2. if (receivedData==0) 不会做你想要它做的事情。该请求异步运行,因此 receivedData 的值永远不会反映您之前启动的 2 行请求中发生的情况。
  3. 您是否试图检查 receivedData 是否为空?如果是这样,请这样说:if (receivedData == nil),它清楚地表明您要在这里做什么。
  4. 在您的 request:didReceiveData 方法中,您似乎正在从请求中获取数据并将其附加到自身上(我不确定当您在请求完成之前访问它可能为空)。无论这里发生什么,我很确定这都不是你想要发生的。

最后,除非您确实想自己聚合和解析传入数据(您可能不想),否则您应该实现这两个方法,而不是 request:didReceiveData 方法。

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

他们在请求完全完成后被调用。通过调用访问服务器响应的字符串值。

NSString *receivedString = [request responseString];

只有在调用 requestFinished 方法后,您才能设置名字 UITextField 的值。

一点建议:您很少需要在 Obj-C 中使用全局变量,并且您当然不需要在这里使用它们。我会尽快摆脱那些。

我对你的意图做了很多假设,所以如果这不是你想要的,请留下评论。祝你好运!

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!

差↓一点笑了 2024-09-20 07:56:01

using (responseData == 0) 只会告诉您responseData是否已分配。

如果我理解你所说的,你需要检查你从网络上得到的响应......

在这种情况下:

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

做一个简单的 NSLog :

NSLog(@"result: %@", receivedString);

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);
夏尔 2024-09-20 07:56:01

首先,在我看来,您似乎正在立即检查请求的结果,但作为异步请求,检查将在请求完成之前进行。

至于您的主要问题,如果服务器响应一个字符(仅 '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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文