如何让这个 NSURLConnection 工作?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个疯狂的猜测:在请求之间设置 10 秒的间隔,只是为了看看他的行为是否有任何变化。
A wild guess: put a 10 second interval between the requests, just to see if there is any change in his behavior.
如果我理解正确并且您想为每个 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.