好的,为了在进行网络通话时在后台为 Android 制作动画,请使用 AsyncTask。看起来像这样
private class WebCallOperation extends AsyncTask<String, Void, String>
{
private final ProgressDialog dialog = new ProgressDialog(context);
@Override
protected String doInBackground(String... params)
{
//web call code here
//response returned here
return "";
}
@SuppressWarnings("rawtypes")
@Override
protected void onPostExecute(String result)
{
if (this.dialog.isShowing())
{
this.dialog.dismiss();
}
}
@Override
protected void onPreExecute()
{
this.dialog.setMessage("Loading");
this.dialog.show();
}
}
在我刚刚给出的示例代码中,我添加了 ProgressDialog,这通常是我在进行网络呼叫时使用的,但是您可以将其替换为您想要的任何内容。您可以添加自己的视图,并为其设置动画。在 pre execute 方法中对其进行动画处理,然后在 post execute 方法中完成 web 调用。
关于 IOS 应用程序的一点建议,我在评论中已经提到了持续时间问题。我还认为您应该研究用于执行网络呼叫的块代码。如果你希望我可以提供一些示例代码,会让你的生活更轻松
编辑:
Objective-C 块代码。在此处创建此类
接口类
#import <Foundation/Foundation.h>
#import "WebCall.h"
@interface WebCall : NSObject
{
void(^webCallDidFinish)(NSString *response);
}
@property (nonatomic, retain) NSMutableData *responseData;
-(void)setWebCallDidFinish:(void (^)(NSString *))wcdf;
-(void)webServiceCall :(NSString *)sURL_p : (NSMutableArray *)valueList_p : (NSMutableArray *)keyList_p;
@end
实现类
#import "WebCall.h"
#import "AppDelegate.h"
@implementation WebCall
@synthesize responseData;
-(void)setWebCallDidFinish:(void (^)(NSString *))wcdf
{
webCallDidFinish = [wcdf copy];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int responseStatusCode = [httpResponse statusCode];
NSLog(@"Response Code = %i", responseStatusCode);
if(responseStatusCode < 200 || responseStatusCode > 300)
{
webCallDidFinish(@"failure");
}
[responseData setLength:0];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"WebCall Error: %@", error);
webCallDidFinish(@"failure");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
response = [response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
webCallDidFinish(response);
}
-(void)webServiceCall :(NSString *)sURL_p : (NSMutableArray *)valueList_p : (NSMutableArray *)keyList_p
{
NSMutableString *sPost = [[NSMutableString alloc] init];
//If any variables need passed in - append them to the POST
//E.g. if keyList object is username and valueList object is adam will append like
//http://test.jsp?username=adam
if([valueList_p count] > 0)
{
for(int i = 0; i < [valueList_p count]; i++)
{
if(i == 0)
{
[sPost appendFormat:@"%@=%@", [valueList_p objectAtIndex:i],[keyList_p objectAtIndex:i]];
}
else
{
[sPost appendFormat:@"&%@=%@", [valueList_p objectAtIndex:i], [keyList_p objectAtIndex:i]];
}
}
}
NSData * postData = [sPost dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSURL * url = [NSURL URLWithString:sURL_p];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if (theConnection)
{
self.responseData = [NSMutableData data];
}
}
@end
然后你打这个网络电话,你这样称呼它
WebCall *webCall = [[WebCall alloc] init];
[webCall setWebCallDidFinish:^(NSString *str){
//This method is called as as soon as the web call is finished
NSLog(@"%@", str);
}];
//Make web call here
[webCall webServiceCall:@"http://www.bbc.co.uk/" :nil :nil];
参见 setWebCallDidFinish 方法,直到 webcall 结束才会调用它。因此,如果您需要在网络调用完成后立即执行一些代码,例如停止动画,请在该方法中调用它。希望有帮助