Note that there are some explanatory texts on larger screens.

plurals
  1. PONetworking in Android/Java - how do I callback to a API class after a call back from the server?
    primarykey
    data
    text
    <p>So I'm writing an Android application in Java based on an iOS application that I am also working on, but this question is more asking about how to communicate callback mechanism (like blocks in Objective-C 2.0) in Java.</p> <p>This application involves networking, authenticating and communicating with a server via an API.</p> <p>I am using this framework: <a href="https://github.com/loopj/android-async-http" rel="nofollow">https://github.com/loopj/android-async-http</a></p> <p>I am trying to encapsulate all of the networking model into classes to make everything clean and easy (it seems so easy in iOS with delegates and blocks, but java doesn't appear to have ANY of these conveniences). So, I am using this as a guide for callbacks: <a href="http://www.gdgankara.org/2013/03/25/android-asynchronous-http-client-a-callback-based-http-client-library-for-android-and-android-smart-image-view/" rel="nofollow">http://www.gdgankara.org/2013/03/25/android-asynchronous-http-client-a-callback-based-http-client-library-for-android-and-android-smart-image-view/</a></p> <p>Now lets say I don't want to make a call from an Activity class, but an API class, which can be called from an Activity class, how can I do this? I know easily how to do this with blocks and delegates in iOS, but how can I do this with interfaces?</p> <hr> <h1>For Example:</h1> <h2>In <strong>iOS</strong> (using a common networking framework called AFNetworking), I have 4 classes:</h2> <p><strong>HTTPClient.h/m</strong></p> <pre><code>+(id)sharedHTTPClient { static dispatch_once_t pred = 0; __strong static id __httpClient = nil; dispatch_once(&amp;pred, ^{ NSString *baseURL = http://baseurl.com; __httpClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:baseURL]]; [__httpClient setParameterEncoding:AFJSONParameterEncoding]; }); return __httpClient; } </code></pre> <p><strong>APILogin.h/m</strong></p> <pre><code>-(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]; } </code></pre> <p><strong>LoginObject.h/m</strong></p> <pre><code>-(id)init { self = [super init]; if(self) { [self.apiLogin loginWithSuccessBlock:^ void (NSArray *loginArray) { //process the array }]; } } </code></pre> <p><strong>LoginVC.h/m</strong></p> <pre><code>... LoginObject *loginObj = [[LoginObject alloc] init]; ... </code></pre> <hr> <h2>So, now what I have so far, using the Android-Async-Http library is:</h2> <p><strong>HTTPClient.java</strong></p> <pre><code>public class HTTPClient extends AsyncHttpClient { public static HTTPClient sharedHTTPClient; public static String baseUrl = "http://baseurl.com"; public HTTPClient { super(); } ... } </code></pre> <p><strong>APILogin.java</strong></p> <pre><code>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&lt;LoginObject&gt; 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 :( } }); } </code></pre> <p><strong>LoginObject.java</strong></p> <pre><code>public class LoginObject { public LoginObject { this.apiLogin.loginWithSuccessBlock(**something something for a callback**); } } </code></pre> <p>Hopefully, I've made it a little clearer what I am trying to achieve. I want to be able to execute some kind of callback block on the object that called the api call, on success. However, it will not always be the same object. LoginObject may have an instance of APILogin.java and so might a different object, so I can't use the second link above in which you can specific a specific class and pass it in and call a method on it, because the classes will be of different types, and Java doesn't have a generic pointer (id or void*) object.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload