NSURLResponse在模拟器中快速出现,但在实际设备上它会在一段时间后出现或返回NULL

时间:2015-06-01 17:49:07

标签: ios objective-c iphone nsurlconnection nsurlrequest

我正在创建一个在每个视图控制器上调用Web服务的应用程序。应用程序在Simulator上运行正常,但在实际设备上,它运行速度非常慢,或者有时会返回null响应,这就是应用程序崩溃的原因,因为来自服务器的响应为空。有没有解决这个问题的解决方案。我对于客观c编程很新。请给我一个解决问题的方法。非常感谢。:)

这里我添加了我的部分代码,它将请求发送到Web服务并收到响应。

{
    NSURL *url = [NSURL URLWithString:@"http://www.someurl.com/somewebservice.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue:@"somehost.in" forHTTPHeaderField:@"Host"];
    [theRequest addValue:@"someUrl/someWebMethod" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Type"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection =
    [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    NSURLResponse *response1 = [[NSURLResponse alloc] init];;
    self.webResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];

    self.response = [[NSURLResponse alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Printing response...\n");
    NSLog(@"%@",response);
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.webResponseData  appendData:data];
}

1 个答案:

答案 0 :(得分:1)

您正在使用#include <sstream> std::string narrow = "narrow"; std::wstring wide = "wide"; std::wstringstream cls; cls << " abc " << narrow.c_str() << L" def " << wide.c_str(); std::wstring total= cls.str(); 。唐&#39;吨。从不,永远不会永远在主线程上同步网络。