我正在调用我的iPhone应用程序(iOS5)本地托管的.NET webservice:
(IBAction)btnCallService:(id)sender {
[XYZActivity startAnimating];
// So I could show activity on my main UI thread.
[self performSelector: @selector(CallXYZSService)
withObject: nil
afterDelay: 0];
}
(void) CallXYZSService
{
NSURL *url = [NSURL URLWithString: @"http://localwinhost/JSON_Service.asmx/GetFunction1"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:1 timeoutInterval:30];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"close" forHTTPHeaderField:@"Connection"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
//[req setHTTPBody: [postStr dataUsingEncoding:NSUTF8StringEncoding]];
myConn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (myConn) {
webData = [NSMutableData data];
}
else
{
NSLog(@"Connection Failed");
}
}
}
-(void) connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *) response{
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *)connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *)connection
didFailWithError:(NSError *) error {
NSLog(@"Service Failed");
}
现在我的问题是它有时会调用服务而我会重新获取数据,有时它并没有发生任何事情......甚至没有超时错误......
我在这里做错了,我正在调用我的webservice asynch ...问题是它甚至没有去任何其他委托方法,只是准备请求,初始化连接然后什么也没发生.... < / p>
是否与以前与网络服务的连接有关?至于测试我在相同服务器上的同一服务上调用2,3个不同的函数...但是这些都发生在不同的viewcontrollers上......所以我在connectionDidFinishLoading方法中初始化新连接并将我的连接设置为nil。
任何人都可以在这里帮助我,如何确保我总是从服务中得到有效响应或某些错误......或超时.... 如果我在Safari中测试MAC上的相同服务,它总是被调用而没有任何问题...... !!!
谢谢, 特立独行
我已将代码更改为
I have changed the code as below NSString *jsonRequest_NE = [NSString stringWithFormat:@"ID=%@&Password=%@",strID,strPwd];
NSString *jsonRequest = [jsonRequest_NE stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Request: %@", jsonRequest);
NSString *myURL = @"http://localwinhost/JSON_Service.asmx/GetFunction1";
NSString *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
但仍然是相同的beahviour,有时它有效它有时它不..... 我搜索了很多......但仍然找不到合适的解决方案...... 任何一个PLZ帮助我heeeeeeeeeeeee !!!
答案 0 :(得分:0)
确保jsonRequest变量中没有任何非UFT8字符 让我知道你是如何管理它的。