Android - Java等效的iOS块回调?

时间:2022-10-22 19:46:05

In my Android app, I'd like to implement success and error callbacks for when I get reading passages from my backend. In iOS, it would look like this:

在我的Android应用程序中,当我从后端读取段落时,我想实现成功和错误回调。在iOS中,它看起来像这样:

In my Passage.h:

在我的Passage.h中:

-(void)getPassagesWithSuccessCallback:(void (^)(NSArray<Passage *> *))success errorCallback:(void (^)(NSString *))errorString;

In my Passage.m:

在我的Passage.m中:

-(void)getPassagesWithSuccessCallback:(void (^)(NSArray<Passage *> *))success errorCallback:(void (^)(NSString *))errorString {
    MyApiInterface* api = [MyApiInterface sharedInstance];
    [api sendGetRequestTo:@"passages" successCallback:[Passage modelListCallback:success] errorCallback:error];
}

In my Android app, I'm using Volley to handle my API requests, but I want to further encapsulate this API interfacing by having a Passage.java class with a public static void method that gets the passages. Something like this:

在我的Android应用程序中,我使用Volley来处理我的API请求,但我希望通过使用带有public static void方法的Passage.java类来获取通道,从而进一步封装此API接口。像这样的东西:

public static void getPassagesForFirebaseUser(FirebaseUser user, Context context) {
        final String url = URL_BASE + "/passages.json" + "?auth=" + user.getToken(false);
        final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // convert JSON into ArrayList<Passage> object
                // pass on this array of Passages in the success completion listener of the method that called this
                // just like iOS does success(passages)
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // convert error to string
                // pass on this errorString in the error completion listener of the method that called this
                // just like iOS does error(errorString)
            }
        });
        Volley.newRequestQueue(context).add(request);
}

Is there any way to get this kind of implementation flow?

有没有办法让这种实现流程?

1 个解决方案

#1


5  

You can use an Interface

您可以使用界面

public interface ICallbacks { 
    public void onResponse(JSONObject response);
    public void onError(VolleyError error);
}

Then in your routine code just put a new instance of Callbacks (depending on ide that you work could autogenerate the methods)

然后在你的例程代码中放入一个新的Callbacks实例(取决于你工作的ide可以自动生成方法)

public static void getPassagesForFirebaseUser(FirebaseUser user,
Context context, ICallbacks events) { 
//here code and call ICallbacks methods
if(result){ events.onResponse(response); }
if(error){ events.onError(err); }
}

ultimately you can call the method with :

最终你可以调用方法:

getPassagesForFirebaseUser(user, context, new ICallbacks(){
    @Override
    public void onResponse(JSONObject response){
        //Success !!!
    }
    @Override
    public void onError(VolleyError response){
        //Error !!!
    }
});

Sorry for my English, hope this help !

对不起我的英文,希望对你有所帮助!

#1


5  

You can use an Interface

您可以使用界面

public interface ICallbacks { 
    public void onResponse(JSONObject response);
    public void onError(VolleyError error);
}

Then in your routine code just put a new instance of Callbacks (depending on ide that you work could autogenerate the methods)

然后在你的例程代码中放入一个新的Callbacks实例(取决于你工作的ide可以自动生成方法)

public static void getPassagesForFirebaseUser(FirebaseUser user,
Context context, ICallbacks events) { 
//here code and call ICallbacks methods
if(result){ events.onResponse(response); }
if(error){ events.onError(err); }
}

ultimately you can call the method with :

最终你可以调用方法:

getPassagesForFirebaseUser(user, context, new ICallbacks(){
    @Override
    public void onResponse(JSONObject response){
        //Success !!!
    }
    @Override
    public void onError(VolleyError response){
        //Error !!!
    }
});

Sorry for my English, hope this help !

对不起我的英文,希望对你有所帮助!