无法在Android studio中导入Java类

时间:2016-01-31 01:47:04

标签: java android android-studio

这是" java"中的目录结构。文件夹,

enter image description here

当我写作时,问题出在我的MainActivity文件中。

import api.gitapi;
import model.gitmodel;

我收到一条警告信息" Unused Import"

在我的MainActivity文件中,我收到了错误,

gitapi git = restAdapter.create(gitapi.class);

public void success(gitmodel gitmodel, Response response) {}

说无法解析符号gitapigitmodel。我试过" Invalidate caches / restart"但问题仍然存在。

感谢任何帮助,提前谢谢!

PS:如果有帮助,我使用 this tutorial

2 个答案:

答案 0 :(得分:2)

您已将-(void)sendTask:(NSURLRequest*)request successCallback:(void (^)(NSDictionary*))success errorCallback:(void (^)(NSString*))errorCallback { NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { MyAPIInterface *__weak weakSelf = self; [self parseResponse:response data:data fromRequest:request successCallback:success errorCallback:^(NSString *error) { NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; if (httpResp.statusCode == 401) { if ([error isEqualToString:@"invalid_credentials"]) { errorCallback(@"Invalid username and/or password"); } else if ([error isEqualToString:@"token_expired"]) { // check if request's auth token differs from api's current auth token NSArray *requestHeaderValueComponents = [[request valueForHTTPHeaderField:@"Authorization"] componentsSeparatedByString:@" "]; NSString *requestAuthToken = requestHeaderValueComponents[1]; // if new auth token hasn't been retrieved yet if ([requestAuthToken isEqualToString:weakSelf.authToken]) { NSLog(@"THE AUTH TOKENS ARE EQUAL"); if (!weakSelf.currentlyRefreshingToken.boolValue) { //lock alreadyRefreshingToken boolean weakSelf.currentlyRefreshingToken = [NSNumber numberWithBool:YES]; NSLog(@"NOT REFRESHING TOKEN"); // add mutable failed request (to change auth token header later) to failedRequests array NSMutableArray *mutableFailedRequests = [weakSelf.failedRequests mutableCopy]; NSMutableURLRequest *mutableFailedRequest = [request mutableCopy]; [mutableFailedRequests addObject:mutableFailedRequest]; weakSelf.failedRequests = [mutableFailedRequests copy]; // refresh auth token [weakSelf refreshAuthenticationTokenWithSuccessCallback:^(NSDictionary *response) { //store authToken weakSelf.authToken = response[@"token"]; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:weakSelf.authToken forKey:@"authToken"]; [defaults synchronize]; //change auth token http header of each failed request and re-attempt them for (NSMutableURLRequest *failedRequest in weakSelf.failedRequests) { NSString *newAuthHeaderValue = [NSString stringWithFormat:@"Bearer %@", weakSelf.authToken]; [failedRequest setValue:newAuthHeaderValue forHTTPHeaderField:@"Authorization"]; [weakSelf sendTask:failedRequest successCallback:success errorCallback:errorCallback]; } //clear failedRequests array and unlock alreadyRefreshingToken boolean [weakSelf clearFailedRequests]; weakSelf.currentlyRefreshingToken = [NSNumber numberWithBool:NO]; NSLog(@"TOKEN REFRESHING SUCCESSFUL"); } errorCallback:^(NSString *error) { NSLog(@"TOKEN NOT REFRESHABLE! HAVE TO LOG IN MANUALLY"); //clear failedRequests array [weakSelf clearFailedRequests]; weakSelf.currentlyRefreshingToken = [NSNumber numberWithBool:NO]; errorCallback(@"Your login session has expired"); }]; } else { NSLog(@"ALREADY REFRESHING TOKEN. JUST ADD TO FAILED LIST"); // add mutable failed request (to change auth token header later) to failedRequests array NSMutableArray *mutableFailedRequests = [weakSelf.failedRequests mutableCopy]; NSMutableURLRequest *mutableFailedRequest = [request mutableCopy]; [mutableFailedRequests addObject:mutableFailedRequest]; weakSelf.failedRequests = [mutableFailedRequests copy]; } } // if new auth token has been retrieved, simply re-attempt request with new auth token else { NSMutableURLRequest *failedRequest = [request mutableCopy]; NSString *newAuthHeaderValue = [NSString stringWithFormat:@"Bearer %@", weakSelf.authToken]; [failedRequest setValue:newAuthHeaderValue forHTTPHeaderField:@"Authorization"]; [weakSelf sendTask:failedRequest successCallback:success errorCallback:errorCallback]; } } else { errorCallback(error); } } else { errorCallback(error); } }]; }]; [task resume]; } gitapi类放入测试源文件夹中,这意味着它们可以由测试文件夹中的其他类导入,但不能在主文件夹中导入(活动生活)。在主源文件夹中创建这些类,您的活动将能够导入和引用它们。

答案 1 :(得分:0)

只需要将这些类分解到包含MainActivity.class的包中,这可能会解决您的问题,您将获得所需的结果。