Ios objective-c在发送到web服务后冻结
我非常努力地编写了一个IPad应用程序,它可以拍摄3张照片并将它们发送到.NETSOAPWeb服务。它发送的照片正确,我可以看到他们在我的服务器。但问题是,当我单击“发送”按钮时,按钮保持蓝色单击效果,应用程序冻结 我相信我把照片发送到webservice是做错了。我也在做所有的发布工作 请看一下我的代码,告诉我哪里做错了Ios objective-c在发送到web服务后冻结,ios,objective-c,web-services,soap,Ios,Objective C,Web Services,Soap,我非常努力地编写了一个IPad应用程序,它可以拍摄3张照片并将它们发送到.NETSOAPWeb服务。它发送的照片正确,我可以看到他们在我的服务器。但问题是,当我单击“发送”按钮时,按钮保持蓝色单击效果,应用程序冻结 我相信我把照片发送到webservice是做错了。我也在做所有的发布工作 请看一下我的代码,告诉我哪里做错了 - (IBAction)buttonClick:(id)sender { recordResults = FALSE; datax = UIImageJPEGReprese
- (IBAction)buttonClick:(id)sender {
recordResults = FALSE;
datax = UIImageJPEGRepresentation(u1,0.6);
resultx = [datax base64Encoding];
datax2 = UIImageJPEGRepresentation(u2,0.6);
resultx2 = [datax2 base64Encoding];
datax3 = UIImageJPEGRepresentation(u3,0.6);
resultx3 = [datax3 base64Encoding];
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<SendData xmlns=\"http://tempuri.org/\">\n"
"<containerId>%@</containerId>"
"<date_>%@</date_>"
"<image1>%@</image1>"
"<image2>%@</image2>"
"<image3>%@</image3>"
"<question1>%@</question1>"
"<question2>%@</question2>"
"<question3>%@</question3>"
"<question4>%@</question4>"
"<question5>%@</question5>"
"<question6>%@</question6>"
"<notes>%@</notes>"
"<transDate>%@</transDate>"
"<userId>%@</userId>"
"<macId>%@</macId>"
"<sFileID>%@</sFileID>"
"</SendData>"
"</soap:Body>\n"
"</soap:Envelope>\n", @"33", @"2013-09-09", resultx ,
resultx2 ,resultx3 ,@"true" ,@"true", @"true", @"true" ,
@"true" , @"3", @"notes", @"2013-09-09", @"123",
@"mymacid", @"sfileid"];
NSLog(soapMessage);
_lbl_result.text = soapMessage;
NSURL *url = [NSURL URLWithString:@"http://192.168.1.57/service1.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/SendData" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection =
[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
//[nameInput resignFirstResponder];
}
-(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(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
NSLog(theXML);
_lbl_result.text = theXML;
[theXML release];
[connection release];
[webData release];
}
你在主线程中创建请求,这样你的应用程序就是freez。你在后台线程中创建请求,然后为用户加载图标…要加载图标,请参见你在主线程UI线程上执行网络代码,因此它将被锁定,因为你正在给它工作,而不是面向UI的工作 我建议采用以下方法: [NSURLConnection sendAsynchronousRequest:队列:completionHandler:] 它应该是同步调用的直接交换,block completionhandler将为您提供状态 例如,通过以下操作,您将删除所有针对NSURLConnection的代理回调 替换:
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
与:
您将希望使用以下方法异步发送请求:sendAsynchronousRequest:queue:completionHandler:
看看您需要在单独的线程上运行服务器调用。它冻结的原因是因为您正在UI线程上运行它。试着用这个
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Run server call
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
});
});
我的问题是我在主线程中将图像发送到webservice。我通过@user352891用上面的答案解决了这个问题。然而,它并没有完全解决我的问题 从Ipad发送3张照片是一个大数据量。不幸的是,我正在观察我发送给webservice的内容,并在标签和日志中显示webservice对象。这就是为什么它会挂起来。它等待来自Hugeee的Web服务的响应
我注释了所有日志并显示到标签。它成功了您能粘贴错误消息吗?感谢没有错误消息,它做了它应该做的事情。它将图像发送到web服务,这是完美的,但应用程序冻结+1以供MBProgressHUD参考,方便的项目,非常易于使用。我是IOS新手。你能告诉我在哪里贴这个吗?我把它换了,但同样的事情发生了
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Run server call
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
});
});