如何让这个 NSURLConnection 工作?

发布于 2024-12-21 02:54:56 字数 1188 浏览 2 评论 0原文

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

NSMutableURLRequest *post = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];        
for (TestVariable *testVariable in variables)
    {
        NSData *testVariable=[self getJSONTestVariable:testVariable];
        NSString *data=[[NSString alloc]initWithData:maintenanceData encoding:NSUTF8StringEncoding];
        [post setHTTPMethod:@"POST"];
        [post setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [post addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [post addValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
        [post setHTTPBody: [data dataUsingEncoding:NSUTF8StringEncoding ]];               TestVariableDelegate *sjd= [TestVariableDelegate alloc];
       NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:post delegate:sjd];
       [connection start];
    } 

我对这段代码遇到的问题是,我尝试发送的 2-3-100 个元素中只有一个被发送,我不确定如何解决该问题。

委托的实现非常基本,它只是以某种方式处理响应并将其保存到数据库中。

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

NSMutableURLRequest *post = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];        
for (TestVariable *testVariable in variables)
    {
        NSData *testVariable=[self getJSONTestVariable:testVariable];
        NSString *data=[[NSString alloc]initWithData:maintenanceData encoding:NSUTF8StringEncoding];
        [post setHTTPMethod:@"POST"];
        [post setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [post addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [post addValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
        [post setHTTPBody: [data dataUsingEncoding:NSUTF8StringEncoding ]];               TestVariableDelegate *sjd= [TestVariableDelegate alloc];
       NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:post delegate:sjd];
       [connection start];
    } 

The issue I have with this code is that just one of the 2-3-100 elements I am trying to send gets sent and I'm not sure how to solve the issue.

The implementation of the delegate is pretty basic, it just processes the response in a way and saves it to the database.

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

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

发布评论

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

评论(2

月朦胧 2024-12-28 02:54:57

一个疯狂的猜测:在请求之间设置 10 秒的间隔,只是为了看看他的行为是否有任何变化。

A wild guess: put a 10 second interval between the requests, just to see if there is any change in his behavior.

清浅ˋ旧时光 2024-12-28 02:54:57

如果我理解正确并且您想为每个 testVariable 发出请求,则应该将 NSMutableURLRequest 定义放入 for 循环中。

If I understood you right and you want to make a request for each testVariable, you should put the NSMutableURLRequest definition into the for loop.

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