所以我正在基于我也在开发的 iOS 应用程序用 Java 编写 Android 应用程序,但这个问题更多地询问如何在 Java 中传达回调机制(如 Objective-C 2.0 中的 block )。
此应用程序涉及联网、身份验证以及通过 API 与服务器通信。
我正在使用这个框架:https://github.com/loopj/android-async-http
我正在尝试将所有网络模型封装到类中,以使一切变得干净和简单(在 iOS 中使用委托(delegate)和 block 似乎很容易,但 java 似乎没有任何这些便利)。因此,我将其用作回调指南:http://www.gdgankara.org/2013/03/25/android-asynchronous-http-client-a-callback-based-http-client-library-for-android-and-android-smart-image-view/
现在假设我不想从 Activity 类进行调用,而是从 Activity 类调用 API 类,我该怎么做?我很容易知道如何在 iOS 中使用 block 和委托(delegate)执行此操作,但我如何使用接口(interface)执行此操作?
例如:
在 iOS 中(使用称为 AFNetworking 的通用网络框架),我有 4 个类:
HTTPClient.h/m
+(id)sharedHTTPClient
{
static dispatch_once_t pred = 0;
__strong static id __httpClient = nil;
dispatch_once(&pred, ^{
NSString *baseURL = http://baseurl.com;
__httpClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
[__httpClient setParameterEncoding:AFJSONParameterEncoding];
});
return __httpClient;
}
APILogin.h/m
-(void)loginWithSuccessBlock:(void (^)(NSArray *responseArray))loginSuccess {
HTTPClient *httpClient = [HTTPClient sharedHTTPClient];
NSURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"/api/login" parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSArray *response = [self.jsonParser parseResponseFromJSON:JSON];
if (loginSuccess) {
loginSuccess(response);
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
[APIErrorHandler handleError:error withHTTPResponse:response];
}];
[operation start];
}
LoginObject.h/m
-(id)init
{
self = [super init];
if(self) {
[self.apiLogin loginWithSuccessBlock:^ void (NSArray *loginArray) {
//process the array
}];
}
}
LoginVC.h/m
...
LoginObject *loginObj = [[LoginObject alloc] init];
...
那么,到目前为止,使用 Android-Async-Http 库的情况是:
HTTPClient.java
public class HTTPClient extends AsyncHttpClient {
public static HTTPClient sharedHTTPClient;
public static String baseUrl = "http://baseurl.com";
public HTTPClient {
super();
}
...
}
APILogin.java
public class APILogin {
public void loginWithSuccessBlock() {
HTTPClient httpClient = QHTTPClient.sharedHTTPClient;
httpClient.get("/api/login", new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONArray response) {
// Successfully got a response
ArrayList<LoginObject> loginInfo = this.jsonParser.parseLoginFromJSON(response);
**NEED TO DO A CALL BACK!! Like with the blocks in iOS**
}
@Override
public void onSuccess(JSONObject response) {
// Successfully got a response
// shouldn't be an object
throw new IllegalArgumentException();
}
@Override
public void onFailure(Throwable e, String response) {
// Response failed :(
}
});
}
LoginObject.java
public class LoginObject {
public LoginObject {
this.apiLogin.loginWithSuccessBlock(**something something for a callback**);
}
}
希望我已经更清楚地说明了我要实现的目标。我希望能够在成功调用 api 调用的对象上执行某种回调 block 。然而,它并不总是同一个对象。 LoginObject 可能有一个 APILogin.java 的实例,所以可能有一个不同的对象,所以我不能使用上面的第二个链接,您可以在其中指定一个特定的类并将其传入并调用它的方法,因为这些类将是不同类型,Java 没有通用指针(id 或 void*)对象。
最佳答案
因此,在尝试很多事情并在网上搜索可能的解决方案后,我找到了自己的答案。我想到的基本上是链接响应处理程序。
因此:
public class APILogin {
public void loginWithSuccessBlock(**final JsonHttpResponseHandler handler**) {
HTTPClient httpClient = QHTTPClient.sharedHTTPClient;
httpClient.get("/api/login", handler);
}
}
public class LoginObject {
public LoginObject {
this.apiLogin.loginWithSuccessBlock(new JsonHttpResponseHandler(){
...
);
}
}
它不是很健壮,因为它不能让我做很多定制,但它可以。
关于java - Android/Java 中的网络 - 如何在从服务器回调后回调到 API 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17204618/