全部!首先,我看到了所有这样的问题,但仍然困惑,这就是为什么我问。 我有一个“翻译”按钮。当我按下按钮时,向服务器发送请求并获得响应。所以在等待答案时我需要显示活动指示条。我尝试了几个代码,但没有任何工作。有人可以帮助我吗?这是我的代码。我的活动指标是activityInd。如何在等待服务器响应时显示活动指示条?
-(IBAction)translate
{
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:[[self textView1] text], @"SourceText",@"8a^{F4v", @"CheckCode",@"0",@"TranslateDirection",@"1",@"SubjectBase", nil];
NSError *error=nil;
NSData *result=[NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSURL *url=[NSURL URLWithString:@"http://fake.com/notRealUrl"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:result];
NSURLResponse *response=nil;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSUTF8StringEncoding ];
NSString *str=theReply;
str= [str substringWithRange:NSMakeRange(51, str.length - 51 - 2)];
NSString *result2=[str stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@""];
_textView.text=result2;
}
谢谢大家!我发现一种方式很适合使用异步请求。 现在一切工作精美。